Transcoding Audio File With An Image

You have an audio file and want to generate a video file for the same. You can do so by transcoding the audio file with an image. We can do this by creating a video stream from the image and attaching the audio stream to it. We use ffmpeg for this process.

Conversion process

Step 1:

Create a video directly from poster image & audio stream.

For example we use the following FFMPEG command in mobile conversion:

ffmpeg -loop 1 -i <<<<INPUT_IMAGE_FILE.jpg>>>> -i <<<<INPUT_AUDIO_FILE.mp3>>>> -crf 25 -c:v libx264 -vf scale=266x150,format=yuv420p -acodec aac -ab 32k -ar 22050 -ac 1 -strict experimental -map 0:0 -map 1:1  -shortest -y <<<<OUTPUT_TEMP_FILE.mp4>>>>

What are the parameters used for video encoding?

Parameter Value Purpose
-loop 1 Repeat the stream for infinite
-i {{INPUT_IMAGE_FILE.jpg}}
{{INPUT_AUDIO_FILE.mp3}}
Input files (image & audio files)
-crf 25 Constant Rate Factor (CRF) tells encoder to attempt to achieve a certain output quality (18 – 28 is decent)
-c:v libx264 Video codec
-vf scale=266×150,format=yuv420p Video filter
-acodec aac Audio codec
-ab 32k Audio bitrate
-ar 22050 Audio rate
-ac 1 Audio channel
-map 0:0 1:1 Map video stream from input file 0 and audio stream from input file 1
-shortest nothing Informs the encoder to use shortest input to mark end of output file

Step 2:

Adjust the audio length to match the video length (audio padding to match video length).

For example we use the following FFMPEG command in mobile conversion:

ffmpeg -i <<<<OUTPUT_TEMP_FILE.mp4>>>> -map 0:0 -map 0:1 -filter_complex "[0:1]apad" -acodec aac -ab 32k -ar 22050 -ac 1 -strict experimental -shortest -y <<<<OUTPUT_FILE.mp4>>>>

What are the parameters used for video encoding?

Parameter Value Purpose
-filter_complex [0:1]apad Pad the audio stream to match video stream
-map 0:0 0:1 Map video stream from input file 0 and audio stream from input file 0

Other parameters are same as in Step 1.

References:

http://ffmpeg.org/ffmpeg-filters.html#format;
https://trac.ffmpeg.org/wiki/Encode/H.264/;
https://trac.ffmpeg.org/ticket/2214;
http://askubuntu.com/questions/352920/fastest-way-to-convert-videos-batch-or-single

Vijayakumar Moorthy

Add comment

Vijayakumar Moorthy