Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > General > Linux, Mac OS X, & Co
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread Display Modes
Old 9th January 2008, 23:58   #1  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
avi2mp4.sh: Convert files to MP4 (H264 or XviD/AAC)

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 ----------------------------
...
Attached Files
File Type: zip avi2mp4-1.1.17.zip (20.8 KB, 239 views)
File Type: zip avi2mp4-1.1.18.zip (20.9 KB, 223 views)
File Type: zip avi2mp4-1.1.19.zip (21.2 KB, 961 views)

Last edited by micmac; 6th March 2010 at 15:25. Reason: New version
micmac is offline   Reply With Quote
Old 12th January 2008, 11:12   #2  |  Link
Adub
Fighting spam with a fish
 
Adub's Avatar
 
Join Date: Sep 2005
Posts: 2,699
Cool, thanks for your work!
__________________
FAQs:Bond's AVC/H.264 FAQ
Site:Adubvideo
Adub is offline   Reply With Quote
Old 12th January 2008, 19:13   #3  |  Link
rootkit
Registered User
 
rootkit's Avatar
 
Join Date: May 2006
Posts: 24
I had a similar tool here: http://teknoraver.campuslife.it/software/mp4tools/
but mine can encode any input, not just .avi and with every aspect ratio.
Would you improve mine instead of writing your own?
__________________
Code:
 .""`.    Matteo Croce
: :"  :   proud Debian admin and user
`. `"`
  `-  Debian - when you have better things to do than fix a system
rootkit is offline   Reply With Quote
Old 17th January 2008, 15:31   #4  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Quote:
Originally Posted by rootkit View Post
I had a similar tool here: http://teknoraver.campuslife.it/software/mp4tools/
but mine can encode any input, not just .avi and with every aspect ratio.
Would you improve mine instead of writing your own?
Hi rootkit!

I looked at your script and figured it'd take me some time to get to know it. Sorry.

In the meantime I've changed avi2mp4.sh to support any file and any aspect ratio as well.

Code:
1.0.5: - Small change to comments
       - Use myidentify instead of midentify; now input pixel aspect
         ratio doesn't have to be 1:1 anymore
       - Function c; unset variables containing essential file
         properties and continue
       - Protect mplayer from input while extracting audio
         (-nolirc -noconsolecontrols etc.)
       - Remove AVI requirement; now any file is possible as input
       - Add VFOPTS variable
1.0.6: - Remove '-noskip -mc 0' again
       - Change "Usage: ..." info
       - Add rough sketch about what the script does and what it can't
         do
       - Add note about iPod (tm) import
       - Print target bitrate and bpp
       - Unset LC_ALL and set LC_NUMERIC to POSIX to make sure we get
         dot instead of comma as decimal separator as bc expects a dot
1.0.7: - Add a little enhancement to function c; ability to return to
         the n-th eclosing loop
       - Fix two funtion c calls returning to the wrong loops
Regards
micmac

Last edited by micmac; 17th January 2008 at 16:56.
micmac is offline   Reply With Quote
Old 18th January 2008, 02:57   #5  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Code:
1.0.8: - Remove myidentify log when skipping file
       - Use rm -f
       - Add possibility to add options to mencoder calls through
         $MENCODER_OPTS

Last edited by micmac; 27th January 2008 at 14:11.
micmac is offline   Reply With Quote
Old 21st January 2008, 18:19   #6  |  Link
microchip8
ffx264/ffhevc author
 
microchip8's Avatar
 
Join Date: May 2007
Location: /dev/video0
Posts: 1,844
http://h264enc.sourceforge.net
__________________
ffx264 || ffhevc || ffxvid || microenc
microchip8 is offline   Reply With Quote
Old 27th January 2008, 14:12   #7  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Code:
1.0.9. - In case aspect ratio is not set simply assume Width:Height
       - Add -endpos .1 to myidentify to prevent playback of audio
         files; thanks to Reimar Doeffinger for the tip
       - Check $VFOPTS for cropping. If detected calculate new aspect
         ratio so we get a proper output resolution
micmac is offline   Reply With Quote
Old 27th January 2008, 14:19   #8  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Quote:
Originally Posted by froggy1 View Post
Hi!

I can't say that I tried it but it seems h264enc isn't what I was looking for. I didn't want an interactive application. Instead I wanted an application for one specific task with batch processing capabilities. That's why I came up with avi2mp4.sh.

Regards
mic
micmac is offline   Reply With Quote
Old 29th January 2008, 22:08   #9  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Code:
1.1.0: - Remove bogus comma from sed call (BASENAME="...")
       - Remove redundant scale command from bc call
       - Check for (and prevent) upscaling of video
       - Lower the bitrate with regards to BPP
       - Move crop detection around
       - Update crop detection for special cases w=0 and h=0; this
         also fixes a potential division by zero
       - Update comments
       - Add CH and CW to function c so both get unset
       - Dimension -> Resolution
       - Display $CW x $CH as resolution if available
       - Make sure ID_VIDEO_WIDTH, ID_VIDEO_HEIGHT, ID_VIDEO_FPS and
         ID_AUDIO_NCH aren't negative
       - Fix the for loop that looks at output resolutions
1.1.1: - Fix behaviour when supplied cropping values are bogus
1.1.2: - Another attempt to correct the behaviour in case of bogus
         crop values; only accept numbers and let mencoder do the
         dying
       - Change loop expression in c-style for-loop once more just for
         correctness' sake
micmac is offline   Reply With Quote
Old 16th February 2008, 10:44   #10  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Code:
1.1.3: - Direct error messages to stderr
       - Minor comment fix
       - If there's a ".p" file treat video as progressive
       - Users can add mencoder options on a per-file basis
       - Add tip about DVD ripping
       - Add clear_variables function
micmac is offline   Reply With Quote
Old 16th February 2008, 12:19   #11  |  Link
microchip8
ffx264/ffhevc author
 
microchip8's Avatar
 
Join Date: May 2007
Location: /dev/video0
Posts: 1,844
Quote:
Originally Posted by micmac View Post
Hi!

I can't say that I tried it but it seems h264enc isn't what I was looking for. I didn't want an interactive application. Instead I wanted an application for one specific task with batch processing capabilities. That's why I came up with avi2mp4.sh.

Regards
mic
h264enc has iPod presets, batch encoding support and support for MKV/MP4 and OGM
__________________
ffx264 || ffhevc || ffxvid || microenc
microchip8 is offline   Reply With Quote
Old 16th February 2008, 13:19   #12  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Quote:
Originally Posted by froggy1 View Post
h264enc has iPod presets, batch encoding support and support for MKV/MP4 and OGM
I like my script
micmac is offline   Reply With Quote
Old 16th February 2008, 13:35   #13  |  Link
microchip8
ffx264/ffhevc author
 
microchip8's Avatar
 
Join Date: May 2007
Location: /dev/video0
Posts: 1,844
of course,.... I was just pointing out to your previous reply on h264enc
__________________
ffx264 || ffhevc || ffxvid || microenc
microchip8 is offline   Reply With Quote
Old 16th February 2008, 16:51   #14  |  Link
microchip8
ffx264/ffhevc author
 
microchip8's Avatar
 
Join Date: May 2007
Location: /dev/video0
Posts: 1,844
you should really add the 'harddup' filter at the end of the filter chain so duplicate frames get hard-coded in the file. This is essential if you're going to remux the AVI to another container so you won't lose A/V sync

also -mc 0 completely disables the A/V sync code of MEncoder so you should really stay away from it
__________________
ffx264 || ffhevc || ffxvid || microenc
microchip8 is offline   Reply With Quote
Old 16th February 2008, 20:07   #15  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Hi!

Quote:
Originally Posted by froggy1 View Post
you should really add the 'harddup' filter at the end of the filter chain so duplicate frames get hard-coded in the file. This is essential if you're going to remux the AVI to another container so you won't lose A/V sync
harddup has been used since the very first version:
Code:
mencoder ... -vf ${VFOPTS}scale=${OUT_HORIZ}:${OUT_VERT},harddup ...
Quote:
Originally Posted by froggy1 View Post
also -mc 0 completely disables the A/V sync code of MEncoder so you should really stay away from it
That's what I'm thinking as well ("-mc 0 -noskip" were removed from the script in v1.0.6).

Regards
mic
micmac is offline   Reply With Quote
Old 16th February 2008, 20:51   #16  |  Link
microchip8
ffx264/ffhevc author
 
microchip8's Avatar
 
Join Date: May 2007
Location: /dev/video0
Posts: 1,844
ahhh must have missed harddup then, sorry bout that
__________________
ffx264 || ffhevc || ffxvid || microenc
microchip8 is offline   Reply With Quote
Old 5th March 2008, 12:23   #17  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Thanks for all the downloads! If you're missing a feature or if there's something else, let me know.

Btw to get around the "long filename crashes" with gpac/MP4Box, recompile it _with_ CFLAGS but without any "-OX" options.

E.g.

Code:
CFLAGS="-march=athlon-xp -pipe"
is fine, whereas
Code:
CFLAGS="-march=athlon-xp -pipe -O2"
is not. This workaround helped in my case.

Regards!
micmac is offline   Reply With Quote
Old 3rd April 2008, 10:52   #18  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Code:
1.1.4: - Add possiblity to set maximum output resolution, thanks to
         L. Pearce for the idea; see comments regarding $MAX_RES
       - Changed comments regarding MP4Box crashes (added suggestion
         for real workaround)
micmac is offline   Reply With Quote
Old 15th June 2008, 20:54   #19  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Code:
1.1.5: - Add $MP4BOX_IPOD to set whether "MP4Box -ipod ..." is run
       - Add possibility to renice the script (alter its priority)
       - use "-oac pcm" instead of "-oac copy"; it does add a little
         overhead (and we need more drive space) but it helps keeping
         audio and video in sync; also some audio codecs cannot be
         muxed into AVI, e.g. Vorbis, so now we're working around this
         issue
micmac is offline   Reply With Quote
Old 26th June 2008, 07:07   #20  |  Link
micmac
Registered User
 
Join Date: Dec 2004
Posts: 40
Code:
1.1.6:
        - Split release into ChangeLog, README, INSTALL and script itself
        - Add possibility to use neroAacEnc AAC encoder instead of faac; it's
          not Open Source but I ran into an issue with faac earlier and
          apparently it's not maintained anymore
        - Increase accuracy of aspect calculation; I doubt this has any effect
micmac is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 16:15.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.