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.

Transmageddon: Linux video transcoder

Transmageddon is a fresh video converter for Linux. It uses GStreamer in the backend. The author has announced the first release yesterday. The goal of this software is to provide a very easy interface to users for transcoding videos into formats recognized by their devices. As this is just the first release there are many exciting features in the TODO list which are yet to be implemented. One of the features already implemented is video rotation which is very useful but not commonly exposed by GUI based transcoder interfaces. Transmageddon supports all formats supported by GStreamer.

To install Transmageddon on Ubuntu, run:

$ sudo apt-get install transmageddon

Webpage: Transmageddon

OpenShot: edit videos on Linux


Linux has a huge array of video editors. One of the newer members is the immensely powerful Lightworks, which has announced the Linux public Beta recently after a long delay. While Lightworks is industry-standard, OpenShot on the other hand maintains a balance between a rich set of features and ease of use for the regular user.It uses ffmpeg and has plenty of features to boost your videos:

  • Support for many video, audio, and image formats (based on FFmpeg )
  • Gnome integration (drag and drop support)
  • Unlimited tracks / layers
  • Clip resizing, scaling, trimming, snapping, and cutting
  • Video transitions with real-time previews
  • Compositing, image overlays, watermarks
  • Title templates, title creation, sub-titles
  • 3D Animated Titles
  • SVG friendly, to create and include vector titles and credits
  • Scrolling motion picture credits
  • Solid color clips (including alpha compositing )
  • Support for Rotoscoping / Image sequences
  • Advanced Timeline (including Drag & drop, scrolling, panning, zooming, and snapping)
  • Frame stepping (key-mappings: J, K, and L keys)
  • Video encoding (based on FFmpeg )
  • Key Frame animation
  • Digital zooming of video clips
  • Speed changes on clips (slow motion etc)
  • Custom transition lumas and masks
  • Audio mixing and editing
  • Presets for key frame animations and layout
  • Ken Burns effect (artistic panning over an image)
  • Digital video effects , including brightness, gamma, hue, greyscale, chroma key (bluescreen / greenscreen) , and over 20 other video effects
  • Extensive editing and compositing features, designed as a practical tool for working with high-definition video including HDV and AVCHD

Install OpenShot on Ubuntu:

$ sudo add-apt-repository ppa:openshot.developers/ppa
$ sudo apt-get update
$ sudo apt-get install openshot openshot-doc

Webpage: OpenShot

Rotate videos using HandBrake

handbrake_compRecently I recorded a video with my mobile inverted. When searching for a tool to undo the rotation I found that HandBrake can do what I wanted. HandBrake is a very popular and powerful multiplatform transcoder that gives fantastic output video and audio qualities at a smaller size if the right parameters are used. It has both GUI and CLI modes. There are many powerful switches in the CLI mode. For example to rotate a video by 90° clockwise, try the following command:

$ HandBrakeCLI -i input.mp4 -o output.mkv --preset=Custom --rotate=4

where Custom is one of the presets you saved in the GUI mode.

You can do the same from the GUI as well, add the following to Video ▸ Extra options (available only for x264 on Linux):

Custom --rotate=4

Shotcut: powerful multiplatform video editor


If you are looking for a powerful and yet free video editor with a stunning GUI and plenty of professional grade functionalities Shotcut is the perfect app for you. It uses FFmpeg and the MLT Framework in the background. It also has libav built-in. Available for Linux, Windows and Mac.


  • supports oodles of audio and video formats and codecs
  • supports many image formats such as BMP, GIF, JPEG, PNG, SVG, TGA, TIFF as well as image sequences
  • no import required – native editing
  • frame-accurate seeking for many formats
  • multi-format timeline: mix and match resolutions and frame rates within a project
  • screen capture (Linux only) including background capture to capture a Shotcut session
  • webcam capture (Linux only)
  • audio capture (Linux only; PulseAudio, JACK, or ALSA)
  • network stream playback (HTTP, HLS, RTMP, RTSP, MMS, UDP)
  • frei0r video generator plugins (e.g. color bars and plasma)
  • Blackmagic Design SDI and HDMI for input and preview monitoring
  • JACK transport sync
  • deinterlacing
  • detailed media properties panel
  • recent files panel with search
  • drag-n-drop files from file manager
  • save and load trimmed clip as MLT XML file
  • load and play complex MLT XML file as a clip
  • audio signal level meter
  • volume control
  • scrubbing and transport control
  • flexible UI through dock-able panels
  • encode/transcode to a variety of formats and codecs
  • capture (record) SDI, HDMI, webcam (V4L2), JACK audio, PulseAudio, IP stream, X11 screen, and Windows DirectShow devices
  • stream (encode to IP) files and any capture source
  • batch encoding with job control
  • create, play, edit, save, load, encode, and stream MLT XML projects (with auto-save)
  • unlimited undo and redo for playlist edits including a history view
  • connect to Melted servers over MVCP TCP protocol
  • control the transport playback of Melted units
  • edit Melted playlists including suport for undo/redo
  • OpenGL GPU-based image processing
  • multi-core parallel image processing (when not using GPU and frame-dropping is disabled)
  • video filters: Blur, Color Grading, Crop, Diffusion, Glow, Invert Colors, Mirror, Opacity, Rotate, Saturation, Sepia Tone, Sharpen, Size and Position, Stabilize, Text, Vignette, Wave, White Balance
  • audio filters: Balance, Copy Channel, Downmix, Gain, Normlize, Pan, Swap Channels
  • 3-way (shadows, mids, highlights) color wheels for color correction and grading
  • eye dropper tool to pick neutral color for white balancing
  • HTML5 (sans audio and video) as video source and filters
  • Leap Motion for jog/shuttle control
  • DeckLink SDI keyer output
  • UI themes/skins: native-OS look and custom dark and light
  • control video zoom in the player: fit viewable area (default), 50%, original (100%), and 200%
  • multitrack timeline with thumbnails and waveforms
  • thumbnail and waveform caching between sessions
  • audio mixing across all tracks
  • video compositing across video tracks
  • trimming (on timeline)
  • append, insert, overwrite, lift, and delete (ripple) editing on the timeline
  • 3-point editing
  • external monitoring on an extra system display/monitor
  • fade in and out audio and fade video from and to black with easy-to-use fader controls on timeline
  • cross-fade audio and video dissolve transitions easily by overlapping shots on the same track of the timeline
  • video wipe transitions: bar, barn door, box, clock (radial), diagonal, iris, matrix, and custom gradient image
  • video quality measurement (PSNR and SSIM)

To install Shotcut, get it from the Download section of the website.

Webpage: Shotcut

Arista: convert video for gadgets

Unlike HandBrake, Arista supports encoding to many more formats. It has presets for many devices. The application is very simple and easy to use. Arista uses GStreamer as the backend transcoding engine.


  • Formats: MP4, WebM, Matroska, AVI, Ogg, FLV
  • Video: H264, VP8, MPEG4/DivX, MPEG2, Theora, Flash
  • Audio: AAC, Vorbis, MP3, FLAC, Speex, WAV
  • Devices: Android, Computer, DVD, iPhone, Ipod, Ipad, Nokia N Series, Web
  • Users can submit presets for different devices
  • Open source and free


To install Arista on Ubuntu, run:

$ sudo apt-get install arista

Webpage: Arista

Calculate HandBrake bitrate

handbrake_compSince HandBrake has removed the option to specify target size in the GUI it has been a pain for many users to calculate the bitrate required to get the same size. However, there is still a way to calculate the bitrate to get approximately the same sized output without any other software.

With the target size option in HandBrake GUI removed, how can you calculate the approx. bitrate for a target size?

My custom setting for audio is Encoder: MP3 lame, Bitrate: 128 and I don’t change it most of the time. When compressing a video I use 5 MB per minute which seems to be of good quality to me.

Let’s try the calculation now. For raw video stream:

video size  in kilobits = length in seconds * video bitrate in kilobits
=> video bitrate in kilobits = video size in kilobits / length in seconds
[PS: it is kilobits]

Here’s an example:

I am having a movie file of 422.3 MB (which is irrelevant right now and for good because nautilus file size calculation in simply wrong nowadays) and on length 37.36 minutes (the part after decimal is just seconds). So i want a target output of approx. 38 minutes * 5 MB = 190 MB = 190 * 1024 * 8 kbits = 1556480 kbits. 37.36 minutes = 2256 seconds. So the approximate bitrate I will use will be = 1556480 / 2256 = 690 kbps.
To compensate for audio stream size, use a bitrate around 20 kbps less then that calculated.

In my observation, 600 to 620 kbps for video and 64 kbps for audio give outputs pretty close to 5 MB per minute.

Get past some common ffmpeg errors

ffmpegThe following two ffmpeg errors are very common and yet I was not able to find a reasonable explanation or fix for at least the first one –
i. Codec type mismatch for mapping …
ii. Number of stream maps must match number of output streams

Explanations –

i. This has to do with the mapping you use for the output file. ffmpeg expects streams in the following order – video, audio, subtitles. So irrespective of the order of streams in input file you should order the streams in the output file accordingly. Example of an working command –

$ ffmpeg -i 25.\ Silence.mkv -scodec copy -acodec copy -vcodec copy 25_\ Silence.mkv -map 0.2 -map 0.3 -map 0.0
ffmpeg version 0.8.3-4:0.8.3-0ubuntu0.12.04.1, Copyright (c) 2000-2012 the Libav developers
built on Jun 12 2012 16:52:09 with gcc 4.6.3
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
[matroska,webm @ 0x1fe97a0] max_analyze_duration reached
[matroska,webm @ 0x1fe97a0] Estimating duration from bitrate, this may be inaccurate
Seems stream 2 codec frame rate differs from container frame rate: 59.94 (2997/50) -> 29.97 (30000/1001)
Input #0, matroska,webm, from '25. Silence.mkv':
title           : [RaX]Death_Note_-_25_-_Silence_[x264_aac]
Duration: 00:22:49.47, start: 0.000000, bitrate: N/A
Stream #0.0(eng): Subtitle: ass (default)
title           : Styled Subtitles (.ass)
Stream #0.1(eng): Subtitle: [0][0][0][0] / 0x0000
title           : Normal Subtitles (.srt)
Stream #0.2: Video: h264 (High), yuv420p, 720x400, PAR 1:1 DAR 9:5, 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)
Stream #0.3(eng): Audio: aac, 48000 Hz, 2 channels (FC), s16 (default)
title           : English
Stream #0.4(jpn): Audio: aac, 48000 Hz, 2 channels (FC), s16
title           : Japanese
Stream #0.5: Attachment: [0][0][0][0] / 0x0000
filename        : Elfar-Normal-G98.ttf
mimetype        : application/x-truetype-font
Output #0, matroska, to '25_ Silence.mkv':
title           : [RaX]Death_Note_-_25_-_Silence_[x264_aac]
encoder         : Lavf53.21.0
Stream #0.0: Video: H264 / 0x34363248, yuv420p, 720x400 [PAR 1:1 DAR 9:5], q=2-31, 1k tbn, 29.97 tbc (default)
Stream #0.1(eng): Audio: aac, 48000 Hz, 2 channels (FC) (default)
title           : English
Stream #0.2(eng): Subtitle: ass (default)
title           : Styled Subtitles (.ass)
Stream mapping:
Stream #0.2 -> #0.0
Stream #0.3 -> #0.1
Stream #0.0 -> #0.2

This works fine as the ordering for the output file is correct – “-map 0.2 (V) -map 0.3 (A) -map 0.0 (S)”.

ii. This error is thrown when your mapped streams exceed the actual number of stream types in input in the output file. For example:

$ ffmpeg -i 25.\ Silence.mkv -acodec copy -vcodec copy 25\ Silence.mkv -map 0.3 -map 0.2

doesn’t work if the input file has a subtitle stream in it. To disable recording the subtitle use the option -sn. Similarly -an and -vn comes handy.

Cmdline Linux multimedia editing

tux_compOften I need to edit multimedia for various reasons. Handbrake is a great friend for quality transcoding but there are frequent instances when I need Swiss-Army knives. For example, removing the non-English audio streams, subtitles from MKV files, extracting audio from one file and inserting into another, saving only a selected region from a video etc. All of these can be done in Linux (resulting in great quality output) with a little bit of understanding of powerful cmdline utilities like mencoder, ffmpeg and mkvtoolnix (in my experience, mencoder and ffmpeg produce better output than mkvtoolnix). You can fine-tune each and every aspect of any multimedia file and control them in the output. They are much faster than any GUI based utility as well. I have used all of these on Ubuntu.

An example of extracting some streams (a video, audio and subtitle) from an MKV file and creating a new file:

$ ffmpeg -i input.mkv -acodec copy -vcodec copy -scodec copy output.mkv -map 0.2 -map 0.3 -map 0.1

Another example of cutting a part of a video file using mencoder:

$ mencoder -ss 01:19:30 -endpos 17:45 -oac copy -ovc copy input.avi -o output.avi

Do the same with ffmpeg:

$ ffmpeg -i input.avi -ss 01:19:30 -t 17:45 -acodec coy -vcodec copy output.avi

Once you use these tools for an hour, you would probably never consider installing bulky GUI applications for multimedia editing once again.