Linux video and audio encoding and decoding

The last weeks, due to some stars alignment i had to do a lot of work with video files. Convertions of formats/containers, codecs, sound adjustments, the works…. first i tried some GUIs in M$ Windows, spent more time than i wanted in installation, testing, de-installing of messy software, and always the overall final results where nothing short of horrible. Then i remember to try in my Linux box ffmpeg, this is a pure command line utilty (no GUI in the middle, but there are some out there), and everything worked out rather good.

So, for my records, before all these commands get lost in history (man history):

Getting info about a file
ffmpeg -i filename.ext

Extracting video
ffmpeg -i inputfilename.ext -vcodec copy outfilename.ext

Extracting audio
ffmpeg -i inputfilename.ext -acodec copy outfilename.ext

Convert aac audio do mp3 (-ar freq in Hz -ab bitrate in bit/s), ex: convert at 44100Hz, 128Kbits
ffmpeg -i filename.aac -ar 44100 -ab 131072 audio.mp3

Convert aac audio do mp3 with volume adjust, good if sound is too high/low, -vol switch (256=normal), ex: 512 two times louder, 128 half sound
ffmpeg -i filename.aac -ar 44100 -ab 131072 -vol 512 audio.mp3

Convert raw avi to M$ compatible vanilla installation (no codecs) of Windows Media Player (-b switch for bitrate quality), example at 10Mb/s (good quality)
ffmpeg -i filename.avi -vcodec msmpeg4v2 -b 10000000 filename.avi

Convert raw avi to M$ compatible vanilla installation (no codecs) of Windows Media Player with frame rate adjust, example to 24 frames per second
ffmpeg -i filename.avi -vcodec msmpeg4v2 -b 10000000 -r 25 filename.avi

Convert raw avi to M$ wmv at 24 fps with the same quality settings of the original file
ffmpeg -i filename.avi -vcodec wmv1 -sameq -r 25 lfilename.wmv

Convert mp4 to avi, with the same quality settings
ffmpeg -i filename.mp4 -sameq filename.avi

Convert flv to mp4, keeping the same quality. Check if the flv audio and video stream is supported by your device. If it is, no need to re-encode, just change the container to mp4
ffmpeg -i filename.flv -c copy -copyts output.mp4

Some files had some kind of problems, and ffmpeg couldn’t parse them spitting some errors, so i give it a try in mencoder, and the son of a gun worked like a charm.

Extract video from file
mencoder infile.ext -ovc copy -nosound -o outfile.ext

Adjust the sound to lower volume
ffmpeg -i file.flv -acodec copy -vn audio.aac
ffmpeg -i audio.aac -ar 44100 -ab 131072 -vol 128 audio.mp3
ffmpeg -i file.flv -i audio.mp3 -map 0:0 -map 1:0 -vcodec copy -acodec copy file2.flv

This is just a scratch in the surface off ffmpeg and mencoder, and many more formats, options, conversions, codecs are supported, so this post can be updated anytime with new info.

Thank you Open Source for saving the day (yet again)!

Updates:

19/10/2013: i downloaded (sorry, bought on iTunes…) a mkv file with built-in subtitles and Dolby DTS sound. My TV Player didn’t like the audio (not supported) nor the built-in subtitles… time to fix this. Install mkvtoolnix and mkvtoolnix-gui

sudo apt-get install mkvtoolnix
sudo apt-get install mkvtoolnix-gui

Get information about file with mkvinfo

mkvinfo file.mkv

Example output (the relevant part):

| + A track
|  + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
|  + Track UID: 1
|  + Track type: video
|  + Lacing flag: 0
|  + MinCache: 1
|  + Codec ID: V_MPEG4/ISO/AVC
|  + CodecPrivate, length 46 (h.264 profile: High @L4.1)
|  + Default duration: 41.708ms (23.976 frames/fields per second for a video track)
|  + Video track
|   + Pixel width: 1196
|   + Pixel height: 720
|   + Display width: 1196
|   + Display height: 720
| + A track
|  + Track number: 2 (track ID for mkvmerge & mkvextract: 1)
|  + Track UID: 1682142618
|  + Track type: audio
|  + Codec ID: A_DTS
|  + Default duration: 10.667ms (93.750 frames/fields per second for a video track)
|  + Language: jpn
|  + Audio track
|   + Sampling frequency: 48000
|   + Channels: 3
| + A track
|  + Track number: 3 (track ID for mkvmerge & mkvextract: 2)
|  + Track UID: 2244222851
|  + Track type: subtitles
|  + Lacing flag: 0
|  + Codec ID: S_TEXT/UTF8

so, 3 tracks. Track number 1 video in h.264, 2 audio in DTS with 3 channels and track 3 subtitles. So first thing, extract audio and subtitles (note that first track is track 0 and so on for mkvextract):

mkvextract tracks file.mkv 1:audio.dts
mkvextract tracks file.mkv 2:subtitles.srt

And there, all done about the subtitles, tbe .srt outputted works as is, no mores fuss with this. Now, the DTS audio not supported must be converted to another format, AC3 works fine with my TV so i converted DTS to AC3 (i had some errors trying to convert directly so i added an extra step as i converted to DTS to WAV and then to AC3 (the DTS bitrate was a whooping 2304Kbps !!! audio.dts was 1.2Gb…. so i downgraded to a pretty good quality and more sensible 640Kbps).

ffmpeg -i audio.dts audio.wav
ffmpeg -i audio.wav -acodec ac3 -ac 3 -ab 640k audio.ac3

Now, i went in for the muxing with the GUI, just run ‘mmg’ and then in the simple interface simply add the mkv and the audio.ac3, select in the tracks the video and the ac3 audio, unselect all the others tracks, and then “Start muxing”.

30/07/2012: a simple php script to bulk edit mp3 bitrate

<?php

$files = scandir('./');

foreach ($files as $file) {
  if (preg_match('/\.mp3$/', $file)) {
    $output = shell_exec('ffmpeg -i "'.$file.'" -ar 44100 -ab 192k "tmp.mp3"');
    rename("tmp.mp3", $file);
    unlink("tmp.mp3");
  }
}
?>

21/03/2011: since the writing of the post, i have been using more and more VirtualDub, it’s Windows only 🙁 but also Open source. Also a pain in the ass to install all the pug-ins, codecs, filters, etc… but after up and running is that easy to decode/encode in various formats.

Leave a Reply