The API described below is outdated. Please download the source code of the latest version and refer to its annotations. The new version was mainly written by John and made many improvements based on the old version.
What is FFMPEG?
FFmpeg is a complete set of recording, conversion, streaming audio and video solutions. It is also an open-source project under the lgpl protocol. It includes industry-leading Audio/Video Codec libraries. FFmpeg is developed in Linux, but it can also be compiled in other operating systems, including windows.
The entire project consists of the following parts:
- FFmpeg: a command line tool used to convert video file formats. It also supports real-time video capturing and encoding from the video card.
- Ffserver: a multimedia server based on HTTP protocol (RTSP-based version under development) for real-time broadcast. It also supports time translation of real-time broadcast.
- Ffplay: A simple media player developed using the SDL and FFMPEG libraries.
- Libavcodec: a library that contains all FFMPEG audio/video codecs. To ensure optimal performance and high reusability, most codecs are developed from scratch.
- Libavformat: a library that contains all the common audio and video formats of the parser and the generator.
Ffmpegwrapper only uses libavcodec and libavformat.
What is ffmpegwrapper?
Ffmpegwrapper:
- Is a C ++ Win32 dynamic library written in vs2005 in windows.
- The object-oriented method is used to encapsulate APIs commonly used in the FFMPEG library for ease of use. Developers do not need to know much about audio/video coding and decoding.
- 99% of the Code conforms to the C ++ standard and can be easily transplanted to other platforms.
- Developed and maintained by farthinker.
Why use ffmpegwrapper?
For developers who do not know much about video codec, using FFMPEG libraries to write applications is definitely a pain point. First, compile the source code downloaded from SVN (FFMPEG only provides the source code ......). If it is compiled in Windows, it will be troublesome (of course, you can also directly use the SDK compiled by others to skip this step ). After compiling a dynamic library that can be used, you will find it difficult to find a suitable document to learn how to use the FFMPEG library, you can only find out how to use the sample code. Then you will find that the problem occurs one by one, and you do not know where to solve it. In short, the learning cost of FFmpeg is very high.
Ffmpegwrapper aims to simplify the calling process of FFMPEG and target the object, reduce the learning cost of FFMPEG, and enable developers who do not know much about video coding and decoding to easily use FFMPEG. However, while simplifying the use, ffmpegwrapper can hardly inherit all functions of the FFMPEG library. Therefore, ffmpegwrapper is suitable for some applications with relatively simple coding/decoding requirements, but not those applications with complicated, flexible, and highly scalable requirements.
How to Use ffmpegwrapper to codec audio and video?
Preparations
First download the ffmpegwrapper library file (if it is used on a non-Windows platform, you need to download the source code and compile it yourself), and then deploy ffmpegwrapper to the project. During deployment, it is recommended that you do not change the FFMPEG folder relative to ffmpegwrapper. if the path of H must be changed, ffmpegwrapper must be modified for the group. in H # include "FFMPEG/include/avformat. h. The specific method for calling the dynamic library is not described here.
Use ffmpegwrapper to encode audio and video
Specify audio and video Parameters
First, you must specify audio and video parameters. Ffmpegwrapper uses ffmpegvideoparam and ffmpegaudioparam to represent audio and video parameters. The following example specifies a FLV video parameter:
// Specify the video parameters, from left to right: width, height, pixel format, bit rate, frame rate ffmpegvideoparam videoparam (352,288, pix_fmt_yuv420p, 400000, 25 ); // specify the video parameters, from left to right: Bit Rate, sampling rate, number of audio channels ffmpegaudioparam audioparam (64000,441 00, 2 );
If no video stream (audio stream) exists in the video, you can initialize an empty ffmpegvideoparam (ffmpegaudioparam ):
Ffmpegvideoparam videoparam (352,288, pix_fmt_yuv420p, 400000, 25); // No audio stream. Initialize an empty audio parameter object ffmpegaudioparam audioparam ();
Initialize the ffmpegencoder object
Initialize the ffmpegencoder object with audio and video parameters:
Ffmpegvideoparam videoparam (352,288, pix_fmt_yuv420p, 400000, 25); ffmpegaudioparam audioparam (64000,441 00, 2); // The parameters are left to right: ffmpegvideoparam, ffmpegaudioparam, encoding output file name ffmpegencoder testencoder (videoparam, audioparam, "test. FLV ");
The third parameter contains the path, name, and suffix of the output file, and is an optional parameter, that is, there can be no output file. However, when there is no output file, you need to specify the codec name in the audio and video parameters, because ffmpegencoder cannot determine what codec is used from the file Suffix:
Ffmpegvideoparam videoparam (352,288, pix_fmt_yuv420p, 400000, 25, "FLV"); ffmpegaudioparam audioparam (64000,441 00, 2, "libmp 3lame"); // The parameters are left to right: ffmpegvideoparam, ffmpegaudioparam, encoding output file name ffmpegencoder testencoder (videoparam, audioparam );
Frame-by-frame Audio/Video Encoding
Before coding, you must call the open method of the ffmpegencoder object to open the corresponding codec and output files:
FFmpegVideoParam videoParam(352, 288, PIX_FMT_YUV420P, 400000, 25);FFmpegAudioParam audioParam(64000, 44100, 2);FFmpegEncoder testEncoder(videoParam, audioParam,"test.flv");testEncoder.open();
Then you can call the writevideoframe (writeaudioframe) method of the ffmpegencoder object to encode frames by frame and output the video (audio) frequency:
// Videoframedata is the parameter testencoder. writevideoframe (videoframedata) of the random * (unsigned char *) type; // audioframedata is the short * type parameter testencoder. writeaudioframe (audioframedata );
If no output is required after encoding, that is, no output file is available, use the encodevideoframe and getvideobuffer (encodeaudioframe and getaudiobuffer) Methods to encode and obtain the encoded data:
// Videoframedata is a uint8_t * (unsigned char *) parameter testencoder. encodevideoframe (videoframedata); uint8_t * encodedvideo = testencoder. getvideobuffer (); // Where audioframedata is the short * type parameter testencoder. encodeaudioframe (audioframedata); uint8_t * encodedaudio = testencoder. getaudiobuffer ();
During the encoding process, you can also obtain the time stamp (PTS) of the audio and video to Process audio and video synchronization (which is not applicable to the absence of output files). The following is an example:
short *audioData;uint8_t *videoData;double videoPts, audioPts;videoPts = testEncoder.getVideoPts();audioPts = testEncoder.getAudioPts();/* output 5 seconds test video file */while (audioPts < 5) {if (audioPts <= videoPts) {audioData = getTestAudioData();testEncoder.writeAudioFrame(audioData);} else {videoData = getVideoFrame();testEncoder.writeVideoFrame(videoData);}audioPts = testEncoder.getAudioPts();videoPts = testEncoder.getVideoPts();}
After coding, you also need to call the close method of the ffmpegencoder object to close the codec and output files and release resources:
testEncoder.close();
Use ffmpegwrapper to decode audio and video
Initialize the ffmpegdecoder object
Initialize an ffmpegdecoder object and input the name of the input file (including the path, name, and suffix ):
FFmpegDecoder testDecoder("test.flv");
Before decoding audio and video frames, call the open method of the ffmpegdecoder object to open the corresponding codec and input files:
FFmpegDecoder testDecoder("test.flv");testDecoder.open();
Then you can call the decodeframe method of the ffmpegdecoder object to decode audio and video files frame by frame:
// The returned value of decodeframe indicates the status of the currently decoded frame: // 0-video frame // 1-audio frame //-1-end of the file or Int signal = testdecoder error in decoding. decodeframe ()
After decoding, you can use the getvideoframe (getaudioframe) method of the ffmpegdecoder object to obtain the decoded video/audio data. The following is a complete example:
Int signal; uint8_t * decodedvideo; short * decodedaudio; ffmpegdecoder testdecoder ("test. FLV "); ffencoder. open (); While (true) {signal = testdecoder. decodeframe (); If (signal = 0) {decodedvideo = ffdecoder. getvideoframe (); // process decoded video data} elseif (signal = 1) {decodedaudio = ffdecoder. getaudioframe (); // process decoded audio data} else if (signal =-1) {break ;}}
After coding, you also need to call the close method of the ffmpegdecoder object to close the codec and input files and release resources:
testDecoder.close();
Note:
- Ffmpegwrapperhas no specific code function for the moment. If you have any function, use ffmpeg1_format Conversion Tool ffmpeg.exe.
- The above introduction only involves some public APIs of ffmpegwrapper. For details about APIs and other details, see ffmpegwrapper api reference (upcoming ).
- Farthinker is only a web developer and has limited knowledge about audio and video. Therefore, ffmpegwrapper must have some potential problems. You are welcome to criticize and correct it.
Download ffmpegwrapper
- Dynamic library file (2008/8/5): ffmpegwrapper (320)
- Source code (2008/8/5): ffmpegwrapper source code (426)
The future of ffmpegwrapper
As mentioned above, I am only a web developer, not a professional in audio and video. I wrote ffmpegwrapper only because there are some simple coding requirements in the project. I hope ffmpegwrapper can help cainiao like me with audio and video, so that friends who are new to FFMPEG can face less detours. Of course, my capabilities and energy are limited. Without more coding/decoding requirements in the future, it may be difficult for me to spend a lot of time improving ffmepgwrapper. But I really hope ffmpegwrapper can continue, so please contact me if you are interested in writing ffmpegwrapper.
The API described below is outdated. Please download the source code of the latest version and refer to its annotations. The new version was mainly written by John and made many improvements based on the old version.
Post: http://weiyinchao88.iteye.com/blog/1414109