Avconv options options, best options

Disclaimer

This Post may be half assed, this is because I am still working things through but on the plus side even if I am wrong it should get you started thinking in the right way.

Formats and containers

You might think it should be simple but it isn’t because of formats and containers

Avi divx xvid mkv mp4 tend to be containers

I say tend to be since a video stream can be mpeg4 and the container may also be called mp4 although the container contains more than just an mpeg4 video stream.

Think of a container like a cable e.g cat6  basically a big long sheaf containing a number of wires, these wires are streams to us  video streams, subtitle streams, audio streams.

With video streams there is usually just one but its possible for there to be another maybe with a different camera angle or something, when dvd’s came out it was an option that most people didn’t use but in theory you could have different cuts. You would see which version you wanted to see.

Audio is a bit more complicated first there is the alternative language a lot of films on satellite have original and local languages so say starwars on german satellite would have german audio and also a second audio stream in english.

secondly there are different audio formats you could have a 5.1 surround system or simple stereo so you might have an enhanced audio track and a simple stereo audio track allowing missiles to fly over you and explosions originating in different parts of the room.  Sound has become really complicated with high end systems with speakers all over the room it wasn’t enough to just cover the corners now you can have sounds coming from low down or high up. So you might hear a plane overhead and a car more on a level with you.

Do we really care, probably not, books are still popular despite the lack of high definition video and surround sound. Video wasn’t bad when we first got vhs or betamax although the video tearing and drop outs and sometimes distorted audio could distract a little from the experience. Once we got DVD we had a clean picture clean audio and thats probably good enough Blueray still is optional and DVD still sells. With online streaming DVD quality is good enough, since we do not want to be buffering , buffering. pause restart buffering. Our eyes don’t care we just want to see the action and depending on the scene the depth of field maybe small anyway so we get precise sharp blur anyway. The film director is getting us to look at the important bits anyway, he directs the audience as much as the crew on set. Hopefully what is left as a final production isn’t missing anything too essential, This is why people who read the book are often disappointed with the film.

Anyway that wasn’t the point of this post its manipulating streams and containers which we are interested in and we don’t want to lose quality if we can avoid it.  As a photographer there is something called the circles of confusion basically if we can see a point as a point we are fine if we see a point as a disk we think its blurry if we are far enough away we see points again if the image is smaller we also see points. Kids they must see pixels the way they like to get close to the screen. What we don’t want to see is squares on screen and this happens when the bitrate is too low to encode all the action. What we tend to have is a keyframe with all the detail followed by difference frames which just show what has changed from the frame before. This kind of encoding is lossy and re-encoding with another lossy codec will tend to degrade the image. These days we don’t care too much about the file size (buffering …) as long as we get a smooth uninterrupted stream.

Now first problem we have is the container your player may refuse to touch it e.g avi files are not playable with itunes it doesn’t have a problem with mp4 files if it understands the streams inside.

The simplest repack is say an mpeg4 video track with an mp3 audio track.

Avconv can do this very fast at over a 1000 frames per second, in the old days decoding encoding and remuxing was a pain to do on a pentium you might get 5 fps processed of a 30fps film so an hours worth of viewing took 6 hours or so. So it would tend to be run overnight and you would hope it worked in the morning. The real pain was 2 pass encoding the first pass was just to find the minimum bitrate to re-encode at. You just got to watch the movie, adding subtitles was worse.

Ok so if the video and audio are in an acceptable format you only need to change the container if either the video or audio are in the wrong format then you need to re-encode the problem stream.  You don’t really want to re-encode a video stream even to the same format unless the device you want to play it on chokes e.g hd content on a small mobile device. These days 1080p can be handled by most modern devices. Even if it does have to downscale to fit your screens resolution.

Avconv seems to do a good job of keeping in sync another plague that used to drive re encoders nuts as the film starts off in sync and the dialogue drifts further and further away.

One problem you may have is that some containers are less versatile than others e.g subtitles can be a problem the popular srt format may not be compatible with your player.  with dvd the subtitles were 2 color bitmaps that were displayed on top of the video. Doing a DVD translation meant finding the bitmaps OCRing them to get the words translating to something similar in another language and re-encoding as bitmaps and putting them on screen at the right time for the right duration.

avconv -i input.avi -vcodec mpeg4 -acodec aac -strict experimental -ab 192000 -ar 48000 -ac 2 -benchmark -threads 3 output.mp4

This conversion in theory should strip the mp4 video file from the avi container and reencode the audio as aac *(from ac3) at 192kb and a sample frequency of 48,000 hz and put it in an mp4 container.  unfortunately the mpeg4 is re-encoded at a low bitrate and it looks terrible same pixel dimension though the audio seems reasonable.

avconv -i input.avi -c copy -map 0 output.mp4

should do a straight copy but i think the audio will still be a problem. This takes a few minutes about 3 as it is a simple copy and just the container is changed unfortunately the audio isn’t compatible with itunes

ffmpeg -i input.avi -acodec mp3 -vcodec copy out.mp4

This one should work but i think its going to degrade the soundtrack to just stereo

avconv -i input.avi -vcodec copy -acodec aac -strict experimental -ab 192000 -ar 48000 -ac 2 -benchmark -threads 3 output.mp4

This one is a copy of the video and a conversion of the audio to acc from ac3 hopefully this should be the one.  processed in 4:40 less than 5 minutes that is a failure too.

my targets are iTunes and html5 the chromecast appletv android and iphone.

avconv -i input.avi -vcodec libx264 -acodec mp3 test8.mp4

Almost there plays in chrome pretty good audio and video but itunes plays a good quality silent movie

avconv -iinput.avi -vcodec libx264 -acodec aac -strict experimental -ab 192000 -ar 48000 test9.mp4

good in iTunes slow encode.

avconv -iinput.avi -threads auto -vcodec libx264 -acodec aac -strict experimental -ab 192000 -ar 48000 test10.mp4

this is more like it but relatively slow to encode works for itunes and google chrome.

avconv -iinput.avi -threads 2 -vcodec libx264 -preset superfast -qp 0 -acodec aac -strict experimental -ab 192000 -ar 48000 test11.mp4

This is a bit of overkill 1.5gb input 7gb output

We have a winner 🙂

avconv -i input.avi -threads 2 -vcodec libx264 -crf 25 -crf_max 35  -maxrate 4M -bufsize 2M -acodec aac -strict experimental -ab 192000 -ar 48000 test12.mp4

Original divx 1.46GB output 472.3 MB Glitch free Good audio no visible artifacts. Slow encoding on a core 2 duo about realtime to be fair.