Develop a C command line application that utilizes the FFmpeg
library, including Opus Codec
for audio encoding support.
This application should be capable of:
- Read a media file containing audio, specifically supporting G711 and AAC codecs.
- Transcode the media file to Opus codec.
- Write into an MP4 file.
-
There are two Opus encoders for FFmpeg:
opus
: This is the native Opus encoder that comes bundled with FFmpeg.libopus
: This is the Opus encoder that uses the official Opus library.
According to official documents opus and libopus,
This is a native FFmpeg encoder for the Opus format. Its quality is usually worse and at best is equal to the libopus encoder.
we choose
libopus
in this project. -
Opus supports only 8000, 12000, 16000, 24000, and 48000 Hz sample rates. It will be resampled if the sample rate of the input audio is not in this list.
-
Install required toolchains
sudo apt install cmake yasm
-
Compile
Opus Codec
library-
Download source code from here (see Official Site)
-
Compile and install
tar -zxvf opus-1.3.1.tar.gz cd opus-1.3.1 ./configure --prefix=/path/to/ffmpeg-transcoding-example/3rdparty/opus make -j4 make install
-
-
Compile
FFmpeg
library-
Download source code from here (see Official Site)
-
Compile and install
tar -jxvf ffmpeg-snapshot.tar.bz2 cd ffmpeg PKG_CONFIG_PATH=/path/to/ffmpeg-transcoding-example/3rdparty/opus/lib/pkgconfig \ ./configure --prefix=/path/to/ffmpeg-transcoding-example/3rdparty/ffmpeg --enable-libopus make -j4 make install
-
-
Check the codec of media files
ffprobe ./inputs/sample1.alaw # Stream #0:0: Audio: pcm_alaw ffprobe ./inputs/sample2.aac # Stream #0:0: Audio: aac (LC), 48000 Hz ffprobe ./inputs/sample3.aac # Stream #0:0: Audio: aac (LC), 44100 Hz
-
Now your folder structure should be like
- ffmpeg-transcoding-example - 3rdparty - ffmpeg - bin - include - lib - share - opus - include - lib - share - inputs - sample1.alaw - sample2.aac - sample3.aac - outputs
-
Compile
mkdir build cd build cmake .. make -j4
-
Run
./ffmpeg-transcoding-example ../inputs/sample1.alaw ../outputs/sample1.mp4 ./ffmpeg-transcoding-example ../inputs/sample2.aac ../outputs/sample2.mp4 ./ffmpeg-transcoding-example ../inputs/sample3.aac ../outputs/sample3.mp4
-
Validate
ffprobe ../outputs/sample1.mp4 # Stream #0:0(und): Audio: opus ffprobe ../outputs/sample2.mp4 # Stream #0:0(und): Audio: opus ffprobe ../outputs/sample3.mp4 # Stream #0:0(und): Audio: opus