Extract, merge audio video tracks with avconv [ffmpeg]

libav_compSometimes I face a problem while transcoding multimedia files with HandBrake. I prefer to keep the audio bitrate at 96 kbps but sometimes the sound quality of the output audio file is compromised. As transcoding takes time, in many cases I extract the audio track from the source multimedia file and merge it with the video track of the HandBrake output. I use avconv for this. Other tools that come in handy (due to codec issues) are mencoder and mkvtoolnix.

Steps with example:

  1. Check the audio track information from the original file
    $ avconv -i original.mp4
    Stream #0.1(und): Audio: aac, 48000 Hz, stereo, fltp, 95 kb/s
  2. Extract the audio track
    $ avconv -i original.mp4  -vn -acodec copy audio.aac
    // To extract the 4th audio track
    $ avconv -i original.mp4  -map 0:3 -vn -acodec copy audio.aac
  3. Extract the video track from HandBrake output (which is in MKV format)
    $ avconv -i handbrake_out.mkv  -an -vcodec copy video.mkv
  4. To convert the audio bitrate to 96kbps:
    $ avconv -i audio.aac -c:a aac -b:a 96k audio96.aac
  5. Copy audio and video tracks to the same directory and merge them. Let’s say that you need to add some delay to the audio as well. The following command does it all
    $ avconv -i video.mkv -itsoffset 00:00:00.2 -i audio96.aac -acodec copy -vcodec copy merged.mkv

That’s it! merged.mkv is the file you want.

2 thoughts on “Extract, merge audio video tracks with avconv [ffmpeg]”


Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s