Video snippets


When you want to deal with a movie collection, you need some good tools to manipulate the files. Very often command-line tools like ffmpeg, mkvmerge, avconv, etc. are better than GUI tools with limited options. This page is a way to keep in memory different usages of those tools...

Merge video files and subtitles in a single file

You get video files on the web and very often, there is no subtitles. You have to find yourself subtitles files (.srt for example), try them as they can be bad synchronized with the movie and you have to tell your video viewer to use this file... For me it is a real pain in the ass because when viewing a movie should be just a just "launch the video" operation, nothing more.

After fighting with lots of srt files for subtitles, I've found that Mastroska container can incorporate srt files. Inserting those files is very easy with a command-line tool called mkvmerge which can be found in mkvtoolnix debian packet.

Here is the way to build an mkv file from an avi (or mp4) and an srt file:

     mkvmerge -o My_Movie.mkv the_movie.avi

And here is it !

Change the aspect ratio for bad encoded movies

Sometimes I grab movies files with very bad aspect ratios. The easy workaround is to use VLC and change the aspect ratio in the interface. But each time you launch the movie, you have to test and find the real aspect ratio. This is also a real pain in the ass... but you can change this by using gentlemen tools.

Just ask mkvmerge to change the declared aspect ratio in the Matroska container:

     mkvmerge -o My_Movie.mkv --aspect-ratio 0:16/9 the_movie.avi

Everything is done with aspect-ratio option. 0 means that we are declaring the aspect ratio for the track 0 (which is often the video track you want to correct). 16/9 is a way to declare a 16/9 ratio. You can use float numbers too:

     mkvmerge -o My_Movie.mkv --aspect-ratio 0:2.35 the_movie.avi

Just use VLC to find the best aspect ratio and just use mkvmerge to have a nice "just launch the video" movie !

Concatenate videos

Want to append a video at the end of another one ? The answer is still mkvmerge with a + !!!

     mkvmerge -o My_Movie.mkv part_1.avi +part_2.avi +part_3.avi

Extract the audio stream from a video

This time you have to use libav-tools if you are under Debian Jessie.

Use avprobe to find the real audio format:

$ avprobe MyMovie.mp4
avprobe version 11.1-6:11.1-1, Copyright (c) 2007-2014 the Libav developers
built on Dec 30 2014 17:31:25 with gcc 4.9.2 (Debian 4.9.2-9)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'MyMovie.mp4':
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2014-11-25 17:23:00
  Duration: 00:51:22.98, start: 0.000000, bitrate: 247 kb/s
    Stream #0.0(und): Video: h264 (High), yuv420p, 1280x720 [PAR 1:1 DAR 16:9], 222 kb/s, 29.97 fps, 30k tbn, 59.94 tbc (default)
    Stream #0.1(und): Audio: aac, 44100 Hz, stereo, fltp, 191 kb/s (default)
      creation_time   : 2014-11-25 17:23:12
# avprobe output

You can find that the audio codec is AAC. Now, you can extract the stream into a m4a container like this:

    avconv -i MyMovie.mp4 -vn -acodec copy MyMusic.m4a


  • -i: option to tell to avprobe that the input file is MyMovie.mp4
  • -vn: do not record video
  • -acodec: tells which audio codec will be used for output. copy means: use the same codec as source.

Be sure to use m4a for file container because some music players don't handle raw AAC streams.

Extract subtitles from an mkv file

The best way to extract some subtitles from an mkv file is probably to use mkvinfo and mkvextract from mkvtoolnix.

First, find what is the "channel" of the subtitles:

mkvinfo myvideo.mkv
+ Entête EBML
|+ Version EBML : 1
|+ Version EBML lue : 1
|+ Longueur maximale de l'identifiant EBML : 4
|+ Longueur maximale de la taille EBML : 8
|+ Type de document : matroska
|+ Version du type de document : 2
|+ Version lue du type de document : 2
+ Segment, taille 98931722
|+ Tête de positionnement (sous-entrées ignorées)
|+ EbmlVoid (taille : 187)
|+ EbmlVoid (taille : 4096)
|+ Informations de segment
| + UID de segment : 0xf5 0xfa 0xbc 0x1b 0x23 0x7f 0x01 0x7a 0xa7 0x8f 0x66 0x7b 0xa8 0x11 0x2f 0x6b
| + Application de multiplexage : libmkv 0.6.5
| + Écriture de l'application : HandBrake 0.9.9
| + Échelle de code temporel : 1000000
| + Durée : 1325.366s (00:22:05.365)
|+ Pistes du segment
| + Une piste
|  + Numéro de piste : 1 (identifiant de piste pour mkvmerge & mkvextract : 0)
| + Une piste
|  + Numéro de piste : 3 (identifiant de piste pour mkvmerge & mkvextract : 2)
|  + UID de piste : 7080
|  + Type de piste : subtitles
|  + Signal de laçage : 0
|  + Identifiant du codec : S_TEXT/UTF8
|  + Langue : eng
|  + Signal par défaut : 1
|+ Grappe

There we can find that our subtitles are on the track 3 but must be called track number 2 with the other tools.

Then, you just extract the subtitles to a .srt file like this:

mkvextract tracks myvideo.mkv