I need to convert AVI files to H264 (MP4). I am successfully doing so with the following:

-i mymovie.avi -c:v libx264 -crf 19 -preset slow -c:a libfaac -pix_fmt yuv420p -f mp4 -b:a 192k -y -an output.mp4

I am calling the command via a C# .Net Winforms applications, although I suspect this will have little impact on the answer.

The Question

How do I add an audio track? I need to add one, as I need to be able to upload the videos to Instagram, which it seems rejects files without an audio track (to my annoyance). In reality I do not need an audio track that is very long. My thoughts were a couple of seconds of "silence" in MP3 format.

I think I will need AAC audio as the output.

For the record I also realise the -an command will provide no audio and I have taken this away in my later attempts.

Attempts so far

I have gathered I will need to use the -shortest command as the audio will be shorter than the video. In reality I will be adding a blank MP3 (or similar) just so while encoding I get an audio stream embedded in the video.

I also have tried using the -map command but without success.


Ok here is the command I have working. I discovered I did not have libfaac in my build of ffmpeg so i switched. I did not need the -map options either as FFMPEG can deal with 2 inputs fine without extra config.

-i test.avi -i music.mp3 -c:v libx264 -crf 19 -preset slow -c:a libvo_aacenc -pix_fmt yuv420p -f mp4 -b:a 192k -y -shortest output.mp4

The differences between my original and what works are:

  • libvo_aacenc replaces libfaac
  • -shortest is added to deal with shorter audio than video
  • -i is used to provide the audio input


The command after some great input in the comments is now

-i test.avi -i music.mp3 -c:v libx264 -crf 19 -preset slow -c:a aac -strict experimental -pix_fmt yuv420p -f mp4 -b:a 192k -y -shortest output.mp4
  • Note that of all the AAC encoders you can use with ffmpeg, libvo_aacenc offers the worst quality. Better to use -c:a aac -strict experimental and a bitrate of over 96k. This is the ffmpeg-internal encoder. – slhck Dec 3 '13 at 11:36
  • How do I add the bitrate or will it take it from the source file? Thanks for improving my answer! Have I done anything else wrong? – Graham Smith Dec 3 '13 at 11:40
  • 1
    -b:a sets the audio bitrate, so in your example you'd only need to change the encoder part. Some minor things: the -y option should go before the inputs, and -f mp4 is not needed. Apart from that it looks good to me! – slhck Dec 3 '13 at 11:43
  • Also see the FFmpeg and AAC Encoding Guide. – llogan Dec 3 '13 at 18:47

You can generate a silent audio stream with the aevalsrc audio source:

ffmpeg -y -i test.avi -f lavfi -i aevalsrc=0 -c:v libx264 -crf 19 -preset slow \
-pix_fmt yuv420p -shortest output.mp4

This is easier than using a blank audio file as an input.

  • How does the -shortest command work with this setup? – Graham Smith Dec 3 '13 at 20:31
  • 1
    @GrahamSmith Apparently it does nothing in this case. I assumed your video has a finite duration and that aevalsrc will generate silence indefinitely, so I added -shortest to make sure the output ends at the end of the video, but it appears that the output will end with the video anyway. – llogan Dec 3 '13 at 20:49
  • Awesome input and great follow up. Thought my comment maybe of use to others reading this. Thanks again. – Graham Smith Dec 3 '13 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.