Skip to main content

Topic: [Linux] Convert files to MP4 (H264 or XviD/AAC) (Read 13887 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
  • micmac
  • [*][*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Code: [Select]                                                        README

           ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
          ||a |||v |||i |||2 |||m |||p |||4 |||. |||s |||h ||        

-------------- Converts video files in batch mode to MP4 ---------------

Where to get it:

Before putting this script to serious use encode a few samples first. is a batch converter. For each file a resolution gets calcu-
lated and a compressibility test determines the minimum bit rate needed
for the level of quality the user asks for.

If this isn't possible with the maximum bitrate the user allows, then
the output resolution gets downsized and a new compressibility test

Once a matching pair of bit rate and resolution has been found the input
file gets converted to H.264/MPEG-4 AVC or MPEG-4 video and AAC audio.
Finally these streams are wrapped into MP4.

Different kinds of subtitles can be added: SRT, SUB, 3GPP (TTXT) and

----------------------------- Requirements -----------------------------

- bc calculator
- faac or neroAacEnc (even Wine and a Windows binary will do)
- mplayer
- mencoder
- aacgain (at least v1.7.0; you can find a Debian package at RareWares:
- MP4Box (part of gpac; v0.4.4 or later required)

------------------------- Compressibility Tests ------------------------

The compressibility test is new. Send me an e-mail with concerns or
ideas you might have.

--------------------------------- Hints --------------------------------

Global options need to be set in a file in your user's home directory,
namely ~/.avi2mp4.conf. An initial configuration file will be created.

MEncoder is used for the video conversion. There are multiple possibili-
ties to influence MEncoder's behaviour. The first is the configuration
variable MENCODER_OPTS. The content will be passed on to MEncoder.

A second possibility is to add MEncoder options on a per file basis.
Put the desired options in a file and name it like the input file name
extended by the suffix ".me" (e.g.

You can add video filters on a per file basis as well. Write them into
a file the same way as before, only this time use the suffix ".vf". Your
filters will be prepended to the existing chain (scale=x:y,harddup). searches the video filter chain for "crop" statements. If one
is found the calculation of the output resolution will be adjusted

You can indicate that a video is Progressive NTSC by creating a file
with the suffix ".p" (frame rate of the output video will be 23.976
frames per second for NTSC input).

--------------------------- MPEG containers ----------------------------

MPEG containers are problematic because they don't contain a proper
index like AVI containers do. This wasn't a problem until the
compressibility test was introduced. Since then relies on
being able to "jump" through the input file. More often then not this
didn't work with MPEG containers.

Because of that input files are now skipped when an MPEG container is
detected. You'll have to re-multiplex the files to, e.g., AVI:

mencoder -ovc copy -oac copy input.mpg -o movie.avi

To rip a DVD simply dump the title you want to convert to your disk

mencoder dvd:// -ovc copy -oac copy -o movie.avi

Finally: movie.avi

-------------------------------- iPods ---------------------------------

You want your videos to play on your iPod?

- obey bit rate and resolution limits
- no "B" frames allowed. Also, with H.264, the baseline profile level
  must be set and CABAC disabled
- AAC's Low Complexity (LC) profile must be used

Videos created with the initial configuration file should work on your
iPod out of the box. Same should be the case for your PSP.

Please also read about the configuration variables "MP4BOX_IPOD" and

----------------------------- Configuration ----------------------------

Get the zip file from VDR Portal
  • Last Edit: 28 March, 2009, 02:05:15 PM by micmac

  • smok3
  • [*][*][*][*][*]
  • Moderator
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #1
from what i read 'Bits per Pixel' is nonsense, better implement some sort of compression test.
PANIC: CPU 1: Cache Error (unrecoverable - dcache data) Eframe = 0x90000000208cf3b8
NOTICE - cpu 0 didn't dump TLB, may be hung

  • micmac
  • [*][*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #2
from what i read 'Bits per Pixel' is nonsense, better implement some sort of compression test.

Can you quote a source on that? Fact is, it works pretty well.

  • smok3
  • [*][*][*][*][*]
  • Moderator
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #3

Bits per pixel is not a valid measurement. Some sources need more bits than others

practical example;
(any film source would look mucho worse at that bitrate for example)

then, even if BPP value would be true for specific sources at specific resolution, this won't scale linearly with resolution, etc.

so, to conclude, it fails because:
- wont scale with resolution
- every source is different from any other source
- wont scale with different encoder settings

3 times false and you are out

p.s. this is just some general observation, i wont use your script, so don't do anything because of me.
  • Last Edit: 02 September, 2008, 07:54:00 AM by smok3
PANIC: CPU 1: Cache Error (unrecoverable - dcache data) Eframe = 0x90000000208cf3b8
NOTICE - cpu 0 didn't dump TLB, may be hung

  • micmac
  • [*][*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #4
Thanks for the pointer!
Code: [Select]
    - remove BPP usage
    - add compressibility check
    - delete INSTALL file (put content into README)
    - clean up README
    - (hopefully) last batch of quoting fixes
    - make scripting style more consistent
    - better sed scripts
    - use $HOME instead of "~" expansion
    - move command substitutions out of [tests]

  • micmac
  • [*][*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #5
Code: [Select]
    - during compressibility test capture mencoder output in a file
      and display content of file in case mencoder exits with an
      error, otherwise user wouldn't know what went wrong
    - used long command options when available
    - don't write/source temporary configuration file
    - don't just source the mplayer -identify output either for that
    - only load necessary variables from configuration file and
      mplayer's identify output
    - use random name for myidentify output, too
    - set sane PATH, IFS and umask
    - clear all aliases and the command path hash
    - prevent core dumps
    - end shebang line with hyphen
    - changed name of array media_vars to all_media_vars and added
      an array called mplayer_media_vars for clarity's sake
    - made parsing of file for variable a function
    - changed the initial configuration a bit
    - refuse to crop repeatedly
    - demand Bash, version 2 or greater
    - added subtitle support (SRT, SUB and VobSub)
    - added $TRACK_LANG so user can set a three digit language code.
      It's not _that_ useful because you can not set it on a
      per-file or even-per track basis. Why did I even add it in the
      first place? :D
    - call MP4Box from within the tmp directory do avoid another bug
      (for some reason MP4Box added something like ".idx" to the
      command line otherwise)
    - some small fixes (comments, README)
    - call MP4Box without "-isma" because it removes the subtitles
    - script now refuses to (try to) convert mpeg containers
    - updated README

  • fracai
  • [*][*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #6
Not to steal an thunder from this script, I'm sure it's nifty, but an alternative is to use Handbrake.  Mac, *nix, and Windows binaries are available, doesn't require the installation of any other supporting tools, and includes a number of encoding presets for iPods, Apple TV, XviD players, etc.

  • micmac
  • [*][*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #7
Code: [Select]
    - now 3GPP text subtitles (.ttxt) are recognized as well
    - new configuration variable SBTL, see README
    - pass TRACK_LANG & video fps along as a global MP4Box argument
    - changed subtitle handling, now for text subtitles the 3GPP
      header is adapted slightly for proper display (I tried to
      follow the findings of this blog entry:
      Thanks for the ideas!)
    - use "-ipod" argument directly when muxing
    - fix subtitle related mistake
    - set vim modeline
    - some cleanups (in README as well)

  • micmac
  • [*][*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #8
Code: [Select]
  - fix quite a few return checks that caused to not terminate
  - adjust ChangeLog's style :)

  • micmac
  • [*][*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #9
Code: [Select]
  - Fixed a few spelling error
  - Changed initial configuration (point out a few differences between PSPs and
  - Fixed the if-block that deals with cropping and updating the aspect ratio
  - Use wildcard when cleaning up instead of trying to delete file by file
  - Changed default config for PSPs/iPods, apparently x264 changed b frame
    behaviour or default config regarding b frames
  - Substitute some more short command options with long ones
  - Recomplicated track language handling again to fix VobSub track languages
    (these are now determined by MP4Box as it should be); the video track does
    not get a language assigned anymore as that was more or less stupid anyway;
    the language code is now applied only to audio and text subs

  • diepod
  • [*]
[Linux] Convert files to MP4 (H264 or XviD/AAC)
Reply #10
Thank you very much for your hard and continuing work.

This script is kickass!