Finally got videos in a format I can put on an iPod in Linux

Gods, finally!

OK, so:

ffmpeg -i [filename] -f mp4 -vcodec mpeg4 -maxrate 1000k -b 700000 -qmin 3 -qmax 5 -bufsize 4096000 -g 300 -acodec aac -ab 192000 -s 320x240 -aspect 4:3 "[dir]/[filename].mov"

Or for a whole directory of files (let’s say they’re all avis):

ls *.avi | xargs -I{} ffmpeg -i '{}' -f mp4 -vcodec mpeg4 -maxrate 1000k -b 700000 -qmin 3 -qmax 5 -bufsize 4096000 -g 300 -acodec aac -ab 192000 -s 320x240 -aspect 4:3 '{}'.mov

[Don't use those.  USE THIS.  —JHM]

That *.avi?  Change that to whatever filemask you need.  So if you have a bunch of YouTube .flvs, you can substitute ls *.flv at the beginning.  Or, just make a file of the filenames you want, and cat them into xargs: cat [file.with.video.names] | xargs....

To transfer them to your iPod, the best bet is apt-get install gtkpod-aac.

And maybe I’ll add more — in excruciating detail, so that people-just-slightly-less-experienced-than-I can make great use of it.

8 Responses to “Finally got videos in a format I can put on an iPod in Linux”

Read below or add a comment...

  1. -maxrate 1000k

    On (only modern?) iPods, you can crank that effer up to 1500k.

    -s 320×240

    And you can take that up to 640×480 for full (rather than quarter) VGA.  I don’t know at what point in the product line 640×480 was offered, so to err on the side of caution (for both bullets in this comment), use the version I put above.

  2. This is suboptimal.  It’s forcing aspect ratio conversion, which I think is due to the -s bit.  I’m now playing with:

    ls [filemask] | xargs -I{} ffmpeg -i '{}' -f mp4 -vcodec mpeg4 -maxrate 1500k -b 700000 -qmin 3 -qmax 5 -bufsize 4096000 -g 300 -acodec aac -ab 192000 -r 25 -s vga '{}'.mp4

    I’ll update the post if this works better.

  3. Yuck yuck yuckffmpeg is not able to natively preserve aspect ratio.  If the width of your input file is 640 or less you can call it without an -s switch.  But if it’s wider, you need to do the math manually to preserve the aspect ratio — run ffmpeg -i [file], then calculate 640/[width], then multiply the height of the input file by that factor.  Of course this fails if the video is taller than it is wide, but fortunately that almost never happens.

    All this means that I’m going to have to write a more complex script to do this automatically.  But the output from ffmpeg differs from version to version, including on that critical line, meaning that parsing the output of ffmpeg -i is going to be installation-dependent — unless something like m/([d]+x[d+])/ works across all versions.

    You know what would be nice?  Tools that are as smart as they are versatile.

  4. Doing that math for ffmpeg does give a 3x multiplier of speed, though.  Until there are rounding problems, presumably.  Gods.

  5. This does the aspect ratio calculation with sed:

    ffmpeg -i [file] 2>&1 | sed -e '/Video: /!d; s/.* ([0-9]*)x([0-9]*).*/scale=3;1/2/' | bc

    With that number, I should be able to plug it into a Perl script.

    Thanks to here.

  6. unless something like m/([d]+x[d+])/ works across all versions.

    It … er … doesn’t.  Older versions separate with a comma, but they put it on a line littered with other commas.  I can take the Micro$oft-type move and just support the most recent version.  Until that changes.  Or I can query the ffmpeg version number before choosing which regexp to use, which will require going through antique versions of ffmpeg to find out when the change was made.  Yuck.

  7. This worked with a video that was 640×360 to start with to start with:

    ffmpeg -i [input] -f mp4 -vcodec mpeg4 -maxrate 1500k -b 700000 -qmin 3 -qmax 5 -bufsize 4096000 -g 300 -acodec aac -ab 192000 -aspect 4x3 -s 640x360 -padtop 60 -padbottom 60 -r 25  [output].mp4

  8. USE THIS:

    Success!  Here’s the fun part: I haven’t found a way yet to have a video playable on both iPod and Linux if it’s not 4×3 to begin with.  For other aspect ratios, the following commands break the video as far as Linux is concerned.

    Calculate the aspect ratio.  This command does it for you:

    ffmpeg -i [file] 2>&1 | sed -e '/Video: /!d; s/.* ([0-9]*)x([0-9]*).*/scale=3;1/2/' | bc

    If this number is less than 1.33, the following code won’t work, and you’ll have to scale relative to the height, not the width.

    Find the native height of the video.  Do that like this, and look for the WidthxHeight number:

    ffmpeg -i [file]

    Call those W and H, respectively.

    With these numbers in hand, do the following calculation (if you have the Google Toolbar, just type the numbers into the search field and the total will be displayed in a tooltip.  If you don’t, you can use the Google search field as a calculator, but you’ll have to hit Enter. :-) )

    (640 / W) * H

    Round that number to the nearest even integer, and call that value N.  Calculate the height and width of the black “letterbox” bars to put at the top and bottom:

    (480 – N) / 2

    Call that value P.

    With me?  What we’ve done is scaled the height by the same factor by which we’re scaling the width, then calculated the size of the letterbox bars to bring the height back up to the native 480.  If the width of the video is 640 to begin with, you can skip the scaling, but if it’s not clear to you which steps are unnecessary in that case, just do them anyway.

    Now we have our command line:

    ffmpeg -i [input] -f mp4 -vcodec mpeg4 -maxrate 1500k -b 700000 -qmin 3 -qmax 5 -bufsize 4096000 -g 300 -acodec aac -ab 192000 -aspect 4x3 -s 640xN -padtop P -padbottom P -r 25  [output].mp4

    When you hit Enter, it should give you the properties of the output stream.  If the output is not 640×480, you did your math wrong.

    As far as Linux is concerned, you’ve distorted your video (try playing it — does it end up squashed?)  But iPods like it just fine and, in fact, I haven’t found a way to get Linux and the iPod to both like the same version.  That means if you have a video that you want to play on both your iPod and your PC, you need to keep two different versions around.  Awesome!

    Anyway, I hope this helps.  If I’ve made a typo, or if WordPress does substitutions in the command lines and break them, of if you find a degenerate case, or if you know a simultaneously friendly-to-Linux and friendly-to-iPod way to do it, please post.

Leave A Comment...

CommentLuv badge