Help - Search - Members - Calendar
Full Version: [Linux] avi2mp4.sh: Convert files to MP4 (H264 or XviD/AAC)
Hydrogenaudio Forums > Digital Audio/Video > General A/V
micmac
CODE
avi2mp4.sh                                                        README

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


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

Where to get it: http://forum.doom9.org/showthread.php?t=133598

Before putting this script to serious use encode a few samples first.

avi2mp4.sh 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
starts.

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
VobSub.

----------------------------- 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:
  http://www.rarewares.org/debian/packages/unstable/)
- 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. movie.avi.me).

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).

avi2mp4.sh searches the video filter chain for "crop" statements. If one
is found the calculation of the output resolution will be adjusted
accordingly.

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 avi2mp4.sh 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
first:

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

Finally:

avi2mp4.sh 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
    XVID_ENCOPTS=max_bframes=0
    X264_ENCOPTS=nocabac:level_idc=30:bframes=0
- 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
"SBTL".

----------------------------- Configuration ----------------------------
...

Get the zip file from VDR Portal
smok3
from what i read 'Bits per Pixel' is nonsense, better implement some sort of compression test.
micmac
QUOTE (smok3 @ Sep 1 2008, 02:28) *
from what i read 'Bits per Pixel' is nonsense, better implement some sort of compression test.


Hey!
Can you quote a source on that? Fact is, it works pretty well.
smok3
http://forum.doom9.org/showthread.php?t=13...=bits+per+pixel

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


practical example;
http://somestuff.org/flashAVC/flvplayer.ph...ne-x640y352.mp4
(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 wink.gif

p.s. this is just some general observation, i wont use your script, so don't do anything because of me.
micmac
Thanks for the pointer!
CODE
1.1.10:
    - 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
CODE
1.1.11:
    - 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
      matter
    - 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
1.1.12:
    - 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
1.1.13:
    - script now refuses to (try to) convert mpeg containers
    - updated README
fracai
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
CODE
1.1.14:
    - 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:
      http://blog.jharding.org/2008/11/subtitles-on-appletv-and-iphone.html
      Thanks for the ideas!)
    - use "-ipod" argument directly when muxing
1.1.15:
    - fix subtitle related mistake
    - set vim modeline
    - some cleanups (in README as well)
micmac
CODE
1.1.16:
  - fix quite a few return checks that caused avi2mp4.sh to not terminate
    properly
  - adjust ChangeLog's style :)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.