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.

Why i hate SOAP

I hate SOAP (not the washing product, but the Simple Object Access Protocol). In theory is very good, a xml document, the WSDL, makes the description of the service, with the valid (and expected) data types, operations and bindings. You just load this WSDL thing in your client, and voilá, you should now automagically have a full feature client to the service, right? Wrong….

Everytime i need to (have to) use a SOAP webservice, there is always something, some tweak, some unexpexted problem to solve. Or is the WSDL format, or is the protocol version, or there is some kind of obscure authentication to do, or encryption, or something with the namespaces in the request, or something…. So, the expected 5 minutes to go, usually turns more into 50 minutes to go, with a lot of traces, network dumps and the works.

Now comes the plain dumbness. Maybe 50% or more of the operations are quite simple, you pass a couple of values and get a simple data back (like a number or a string), and 90% or more of the operations, the request is quite simple, some strings and numbers acting as filters, and the response is a structured data. So, in all this situations you were much better off with a simple REST interface with GET/POSTs to a JSON response. Light, simple and elegant.

Not with this over bloated protocol, with all the tweaks, versions, revisions and recommendations, messy client/servers, excessive formality. You can grasp how bad it is, simply by looking at a typical SOAP session (request/response), there you find that most of the bytes that are going back and forth are not the actual data, but the SOAP envelope, body, header, xml tags, etc….  its like you are working for the protocol and not the other way around.

No surprise it was developed by Microsoft….

Ensitel – What the fuck?

Quick digest:

  1. Customer gets a phone from Ensitel.
  2. Phone is faulty and client goes to Ensitel to exchange phone.
  3. Customer is sent to Nokia to make exchange because at Ensitel there is no available phone.
  4. At Nokia customer can get a repair but not a new phone.
  5. Back to Ensitel, a new phone comes up, but the exchange is denied because the faulty phone is scratched on the phone (Ensitel can see the scratch, customer can’t).
  6. Customer wants to return the phone and get the money back.
  7. Customer is again denied to a scratch in the battery cover.
  8. Customer goes into legal action against Ensitel.
  9. The case customer Vs Ensitel goes to trial after several months and expenses.
  10. At trial at a Consumer Center, the judge/referee orders the customer to deliver the phone to Ensitel to repair.
  11. Customer ignores trial delivers phone directly at Nokia to repair.
  12. Customer writes about this situation at own blog.
  13. Ensitel starts legal action, against customer to remove blog posts about Ensitel.
  14. Customer writes yet another post about the legal action.
  15. A big reaction starts in the social networks, condemning Ensitel behavior.
  16. Eventually it breaks out to mainstream mass media, tv, radio, newspapers.
  17. Ensitel makes heavy threats to sue customer.
  18. Customer starts a donation campaign to pay legal expenses.

So, as by now, this novel is not yet closed… and further developments are to be expected.

Who are the parties involved?

Ensitel: a company member of Avenir Telecom, a French based multinational, a corporation quoted in the stock market. So, this is what i call a company without a face. A bunch of guys in suits, members of a board of directors, CEO, CFO, etc… all focus in pushing the delivery of results to the stockholders and themselves.

Maria João Nogueira: an individual person, that works on the SAPO Blogs team. She has a wide audience blog and is wife of E. Pinto (SAPO CTO or senior tech something). So definitely not some lame blogger.

The Good

The customer resistance, against the big corporation, the juridic system. Never broked her spirit. How many of us, have been abused by big corporations, that can get away just because they can, and it takes too much time or effort to fight.

The Bad

Of course the juridic system and consumer protection system. Its simply unreasonable. The customer must must fight with the same weapons than a big corporation that is in a very upper hand. Its expected that the consumer, rules out first all the extra judicial mechanisms, then you should hire a lawyer and sue the corporation in a consumer center. Then if the corporation doesn’t accept you should go to common court….The corporation “burns” some hours of some legal services contract, and the consumer a large amount of time and money.

In the end, after months (sometimes years) it doesn’t solves anything,

The Ugly

Ensitel behavior. First its quite obvious that there are superior orders to deny or delay a phone exchange. Then it shields behind the inefficient portuguese consumer protection. Afterwards, when the customer tells the world about it in a blog (ok, one with a large audience), it “burns” some more legal contract hours in a legal action to force the customer to remove the blog posts. If, there is no limits to plain stupidity when the bomb blows up and the hype in the social networks build ups and crosses to above the line, they don’t do any kind of damage control, some rather lame announces on their own Facebook page. Hello!! Is there anybody with a brain in the house?

The cherry on top of the cake is the very strong menaces to the customer.


© Expresso

I already have made a symbolic donation, because i identify myself in this fight against big corporations and know in first hand how hard it is to play in a field that is so much leaned to one side. The Ensitel reputation is down the mud, specially online (see here too).

Anyway, the key point will be at the end the court decision, that should answer both of these questions. Is there any special limitation in the personal online publication of facts and opinions? If one publishes negative content about some corporation, can one be liable of brand depreciation? If yes, to what extension?

So let us just wait and see.

Update
as this post was still a draft, Ensitel as dropped the legal action against Maria João Nogueira, so we will not have a legal decision. For sure, this or other evil corporation will mess up bad again, and an angry customer will publish the story online. In other situation, with much less hype probably the final outcome wouldn’t be the same…

Geek factor? Trough the roof….

A colleague at work send an email about this wonderful piece of software. Logstalgia (aka ApachePong) – a website access log visualization tool. It plays (or replays) the Apache access logs as a battle between a incoming torrent of requests and a pong paddle as the server.

Sample code, with regards from ssh, and unix pipes (in full screen glory):

ssh username@ns1.kolorbit.com tail -f /home/userdir/logs/access.log | logstalgia --sync -f

Utility – None
Geek factor- Trough the roof