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.

Eye protection goggles

I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).
I will always wear my eye protection goggles when doing in hazardous work (even for short time).

So help me God.

European first aid training course

Just finished last week a first aid training course at Portuguese Red Cross. It’s a 12h course with a hands on approach, on subjects like burns, bleedings, choking, victim transportation, getting help, bonds, cpr, and so on. This is a very usefull course, with (in)formation that can make a real difference in some emergency situations in someone’s (can be yourself) life and/or life quality.

The question is: Why in 12 years of mandatory school you actually don’t learn anything about this issue?

You have to learn so many useless stuff – best regards to my History teacher and the Barbarians invasion for example – and this knowledge is kept out of the formal educational system.

Car care – AC clean and spare tyre stow

The AC clean is a very important procedure to get a good air quality inside a vehicle, it can avoid nasty bugs like Legionella developing . It is a very easy procedure, any DIYer should be able to do it with good results and saving some good money (really dont understand how some shops charge over 50 euros for this…). So, what you need:

  • AC cleaner product – i use the 1z klima-cleaner due to very good reviews online – you can get it at a paint shop or car parts shop .
  • Philips screwdriver
  • New cabin filter with charcoal element (if outside big cities get the simple no need of the charcoal).

So first, gain access to the cabin filter and remove it. The Golf 4 is very easy, lift rain rubber seal, remove the screws that hold the filter cover (located in engine bay in front of passenger seat), press clips of the filter frame and pull out. With the old filter out of the way, clean everything up (dust, leaves, cigar butts…).


© Volksbloggin

Now comes the very hard part (not), shake the AC cleaner can and insert the hose that comes with it all the way down the AC system thru the openings revealed by the filter remotion. Use half of the can there, the other half use evenly in the air vents. Remember, AC off and air blower off, open the vents and select in the dash the corresponding air direction as you deliver the product in each vents/section. After the can is empty, put the new filter in place (installation is the reverse of the removal). Then, open the windows and let AC pump air for 10 minutes with nobody inside. You can now enjoy the fresh air inside your car, so go buy something pretty to yourself with the money you have just saved.

I also cleaned up the mess that was the spare tyre compartment. I had a flat (a big rupture at tyre wall) and needed to change the tyre at the highway shoulder, so the wrecked tyre had lots of debris attached to it, brake dust, road dust, bits of tyre rubber, etc that were laid up to the spare tyre compartment. Everything normal, i just don’t understand why when afterward you get the car to the tyre shop they couldn’t care less about that mess, and don’t even pass up with a rag… what a pigs… so unless you want to carry at the trunk brake dust, bits of burned rubber in decomposition and other things that are not good to your health, its up to you to clean up, its very easy and need simple tools:

  • Portable vacuum cleaner
  • Water
  • Nivea lotion

Remove the spare tyre, wash it up with water. Let it dry. Vacuum all the shit of the tyre compartment and clean up with a rag. When the tyre has dried apply a coating of Nivea lotion to the rubber, better more than less. Check and inflate if needed. Now you can store the spare tyre and rest assured that will serve you good in case of need.

About the fuel prices

The fuel prices in the latest weeks are going up, and up, and by now at the pump we are paying around 1.50 euros for 1 liter/gas and 1.30 for diesel and everybody complains because they are too high. Well, i think that the prices should be much higher (50% or more), and that the price difference between gas,diesel and LPG is pathetic (this can be a complete new subject for a complete new post one of these days).

So, why? The last time i checked i’m not masochist, and my daily commute of 50 kilometers would indeed become more expensive. But there is rationale to tax more and get the prices higher, for starter to maintain a stable price for a period of years. Let’s say the price is set a 2 euro/liter, this would be the price for 10 years or a legislature or a relevant time frame. The extra tax collected when the oil prices are runing low, would be used to mantain the price when the oil price goes skyrocket. This would take the fuel prices uncertainty out of equation, for entrepreneurs doing business plans, for big companies, for small companies, for families budgets, for individuals. This would push the adoption of new oil free technologies. This would punish much more consumers who make non-eco choices. This would also reduce unnecessary trips (aka Passeio dos Tristes), and reduce overall average driving speeds and style to more normal values (yes, going 140km/h+ at 50cm of the front vehicle bumper and flashing ligths is only considered normal here and in Morocco…), hence reducing accidents and road mortality.

About the adoption of new technologies, remember that the true kick-start of the industrial revolution, was not the steam engine invention, but the end of the slavery, that dried up a tremendous free work force source and pushed the adoption of the new technology. This is the human way of doing things, when pushed we tend to find a way (a better way) and move forward.

As JFK challenged a country to put some guys in the moon, back in the 60’s, and this is the inspiration for it:

We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard, because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one which we intend to win, and the others, too.

I believe this generation should accept the challenge of shift from oil to clean energy resources, by the end of this decade, not because its easy, but because its hard. Is the right thing to do, for us, for the environment, for our long term wealth, health and safety.