Snippet: convert Hi-def videos to pandora using mencoder (Linux)


Antartica

Member
Joined
Oct 17, 2011
Messages
30
I have used this little script to convert from "1280x720 TV episodes in 10bit h264/mkv" to "800x450 AVI (xvid)", so the pandora can cope with them. I have tested it in a 1Ghz pandora with default settings, and also at 600 mhz, so it should work ok in a classic/rebirth one too.


In the hopes that this is useful for someone :) .


For linux:


encode.sh



Code:
#!/bin/sh

mencoder "$1" -oac mp3lame -ovc xvid -vf scale=800:450 -xvidencopts fixed_quant=4 -o "$2"





It would be used as:





Code:
./encode.sh my720pvideo.mkv mypandoravideo.avi



For windows

I suppose that the following would work with the mencoder for windows, but I have not tested it:



encode.bat



Code:
mencoder %1 -oac mp3lame -ovc xvid -vf scale=800:450 -xvidencopts fixed_quant=4 -o %2





It would be used as:





Code:
encode my720pvideo.mkv mypandoravideo.avi
 
Last edited by a moderator:
Have not timed it.


I do the recode in a i3@3.1Ghz, and mount the "work partition" via NFS in the pandora. I just wait a couple of minutes and start watching the episode while it is transcoding, as it is faster than realtime for me... O:)
 
If you want to do batch conversion, here's a readably-simple rewrite that'll take a list of paths and output the results (same filenames but with .avi extensions) to the current directory.



Code:
#!/bin/sh


# Scale the video to the largest width and height that will fit while

# preserving the aspect ratio. Also, disallow upscaling so that this can be

# used to transcode to a less demanding codec without losing hardware upscaling

# for smaller videos on the Pandora.

VF_OPTS="dsize=800:480:2,scale=0:0::::::1"


for fpath in "$@"; do

    src_filename="${fpath##*/}"	   # Strip the path off the filename

    src_basename="${src_filename%.*}"  # Strip the extension off the filename


    mencoder "$fpath" -oac mp3lame -ovc xvid -vf "$VF_OPTS" -xvidencopts fixed_quant=4 -o "$src_basename.avi"

done


Even with the original version, I did notice a little framedrop stuttering in extremely motion-heavy scenes on my Rebirth pandora but, for the most part, no problem. (How often does an anime have a camera zooming through a blizzard anyway?)
 
Last edited by a moderator:
Without having experience to compare the resulting qualities I want to share a line that I used for a conversion-test a long time ago and was pleased with the result. Can't remember on which formats I used it though.



Code:
ffmpeg -i inputfile -sameq -s 800x480 outputfile
 
Last edited by a moderator:
Without having experience to compare the resulting qualities I want to share a line that I used for a conversion-test a long time ago and was pleased with the result. Can't remember on which formats I used it though.



Code:
ffmpeg -i inputfile -sameq -s 800x480 outputfile

Wouldn't that limit the set of possible output formats based on what the input format is? ...or is that assuming that everything will be MPEG enough to all be able to share the same quantizers? (eg. MPEG-2, DivX/Xvid, H.264, etc.)


I kind of doubt WebM (for example) would do well with those encoding parameters. VP8 is very much NOT a codec of the MPEG lineage.


Also, I'd be careful there since I could easily imagine that causing stuttery playback if the quantizer used in the source file, at 800x480, was simply too CPU intensive for a given pandora.


(My goal is to produce a script I can just trust to be Good Enough™ without me having to babysit every encode.)
 
I haven't used the command which I posted that often and I haven't got a lot of knowledge in video-convertion in general. I know there are tons of containers, codecs, bitrates, ... and a hell of other aspects that are important, but for example I don't really know what these "quantizers" are for. The only additional info I can give is that the ffmpeg-command with just an input- and output-file can be used to convert for example a flac into a 128kb/s-mp3. But maybe both formats are related close enough to each other to make this work and of course video-encoding is a (more or less) complete different thing. Still I can imagine that ffmpeg does some slightly intelligent automation to get a decent result. Well I can also imagine that it doesn't. ;)


Your command is probably the better (more versatile) one as you seem to have more insight on this topic. If I find a large, free VP8-file, then I will try to convert it with ffmpeg and see what the result looks like.


Edit: Made some quick test and converted a 480p-webm-movie into an .avi using ffmpeg without further parameters. The result wasn't really good. Visual was much more blurry/blocky (not quite sure how to describe it). So, yes, better use ssokolows script.
 
Last edited by a moderator:
Antartica is the one who provided the original mencoder command line. I just:

  • Tweaked the scaler so it doesn't assume the input has a 16:9 aspect ratio.
  • Added a restriction that, if the file is already small enough to fit the Pandora's screen, it won't be upscaled. (Might as well let the Pandora do that in hardware rather than needlessly storing and decoding a bigger video file)
  • Added a snippet to generate the output filename from the input one
  • Wrapped it up in a for loop so you can do batch conversion


My specialty is more in the realm of automation but, since I'm pretty good at searching and summarizing, I can explain quantizers and "-sameq":

  • Think of a quantizer as a very fancy form of rounding. You've got a video (or audio) stream and you need some method for rounding the pixels so they don't look that different but can be described in fewer bytes.
  • Like rounding, quantizers inherently lower the precision of the data, so it's important to design them to be able to find a match that looks (or sounds) as close to the original as possible. There are many different ways to do this because it's not considered to be a solved problem. (This is why JPEG artifacts show up in non-photo data. The quantizer used by JPEG assumes skintone-like noise that its effects can hide in.)
  • Converting between MP3 and Ogg Vorbis or H.264 and WebM makes things muddier because they use very different quantizers which means they preserve different aspects of the original signal and, naturally, you can't reconstruct what a previous encoding phase discarded.
  • -sameq means "If the source and destination format both support using the same quantizer, then use it so the transformation will be as close to lossless as possible." but says nothing about what to do if they are too wildly different. (Hence why converting WebM to XviD looks ugly with just "-sameq" and no other options. XviD doesn't support the quantizer used by the source file.)


At some point in the future, I may do the research necessary to improve the encoding but, at the moment, I'm too busy. (I'm not even sure I'll have time to track down why MEncoder drops frames, wrecking audio-video sync, with one specific file I have but none of the others while MPlayer plays it back fine.)


Oh, and here's one benefit to using Mencoder over FFMPEG. If you've customized your ~/.mplayer/config with things like slang=eng (Automatically display english subtitles if available), then MEncoder will automatically convert your preferred subtitle track to hard subs whenever you encode a soft-subbed file to AVI for Pandora playback.
 
Back
Top