You are here: Home » Engineering » Transcoding Audio File With An Image

Transcoding Audio File With An Image

Listen to this article

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?

ParameterValuePurpose
-loop1Repeat the stream for infinite
-i{{INPUT_IMAGE_FILE.jpg}}
{{INPUT_AUDIO_FILE.mp3}}
Input files (image & audio files)
-crf25Constant Rate Factor (CRF) tells encoder to attempt to achieve a certain output quality (18 - 28 is decent)
-c:vlibx264Video codec
-vfscale=266x150,format=yuv420pVideo filter
-acodec
aacAudio codec
-ab32kAudio bitrate
-ar22050Audio rate
-ac1Audio channel
-map0:0 1:1Map video stream from input file 0 and audio stream from input file 1
-shortestnothingInforms 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?

ParameterValuePurpose
-filter_complex[0:1]apadPad the audio stream to match video stream
-map0:0 0:1Map 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

What are your thoughts on this article? reach out to us at info@ventunotech.com if you would like to discuss! 

Looking to launch your streaming app?

Vijayakumar Moorthy

Vijayakumar Moorthy