View Full Version : Scripts to automate MPEG-4 conversion?
Huge
28th September 2004, 21:17
Can anyone share some scripts for converting any type of video into something like divx?
My problem is that I've collected various types of video over the years including my own home movies source in DV format and I'm running out of disk space. I'd like to have a nice way to say, convert this whole directory to xvid or divx for example. I'd want the source to be as good as the original, leave the frame size and AR alone, perhaps convert the audio to something like AC3 but more than likely leave it alone. Perhaps even something that can work out if input format is x and current bit rate is y then .....
I guess the challenges are to figure out what the bitrate should be and skip videos that are already in the target format.
Does anyone have any ideas or existing scripts they want to share?
Huge
29th September 2004, 11:46
Ok, so here's a start. I took two scripts I found and modified them a little. It seems to work OK but I'd like some advise on the transcode parameters.
#!/bin/sh
VCODEC=divx5
ACODEC=
# Create a directory with today's date and the codec name
DIR=$(date +%Y-%m-%d)-$VCODEC
echo Creating the target directory $DIR for the encoded files
mkdir $DIR
FLAGS="-I 3 -C 2 -z -k -w 8000 --print_status 10 -J smartdeinter=diffmode=2:highq=1:cubic=1"
# Set up a for loop that picks out each *.avi (or *.dv) file in turn:
for i in *.avi; do
# This string is for the first pass -- no audio,
R1="-y $VCODEC,null -o /dev/null -R1"
# This is the string for the second pass; it adds sound and creates the mpeg4 file
R2="-y $VCODEC,$ACODEC -R2"
# Run transcode on each file
nice transcode -i "$i" $FLAGS $R1
nice transcode -i "$i" $FLAGS $R2 -o "$DIR/$i"
# Complete the for loop
done
exit
# END
evade
30th September 2004, 04:51
Hi,
You will need to treat interlaced and non interlaced sources differently.
If your sources are interlaced DV and you want to retain as close to full quality as possible you will want to encode interlaced rather than de-interlacing.
I would higly reccomend using XviD with a custom quant matrix.
Here is a script that will encode all the avi and dv files in a your working directory by first performing a 5% compressibility test and using 80% percent of that value to do a two-pass encode. Here also are the quant matrices referenced in the file.
http://mencoder.szczuka.com/example.pl
http://mencoder.szczuka.com/xvid-hvs-sixofnine.intra
http://mencoder.szczuka.com/xvid-hvs-sixofnine.inter
Huge
1st October 2004, 07:59
Thanks for the script, I don't mind if I use divx or xvid to be honest as they can both provide very good quality.
The script doesn't work too well for me though, it get's into an endless loop while trying to calculate the bitrate during the compressibility test. It looks like it works on the movie for a while and goes very slowly, printing some output every 20-30 seconds or so. Then it just goes nuts - the seconds counter increases and it prints output but never stops. I guess it never sees the string it looking for to tell it that it's done... and just keeps going.
<Edit>
I did a little digging and it seems that no matter what value you give it for ss, it still returns a bitrate which is why this never completes.
e.g.
mencoder -oac copy -ovc xvid -o /dev/null -ss 99999 -endpos 00:06 -xvidencopts fixed_quant=2:trellis:chroma_me:vhq=4:autoaspect:interlacing:quant_intra_matrix=/home/dave/xvid-hvs-sixofnine.intra:quant_inter_matrix=/home/dave/xvid-hvs-sixofnine.inter Naples-FL-Beach.avi 2>&1 | grep 'Video stream'
Mendoer report "Video stream: 973.666 kbit/s (121708 bps) size: 4061 bytes 0.033 secs 1 frames" which is wrong.
Huge
1st October 2004, 10:03
OK - I just learned a little Perl :-)
If you add the following line after $k=`mencoder.... then it checks for the text 1 frames which is what mencoder is returning for me. It then just wipes out the return string so the loop can end.
if ($k =~ /1 frames/) {$k="";}
evade
1st October 2004, 12:00
Probably differences in our versions of mencoder as the original script worked for me. ;>
I'm glad you got it working though.
evade
1st October 2004, 12:20
Although the line you probably want to add is this:
if ($k =~ / 1 frames/) {$k="";}
Then it will only match on ' 1 frames' and not on '95541 frames'
I've added that line to the original script.
Huge
1st October 2004, 14:28
So this script seems to keep interlaced as interlaced in xvid right? What if I wanted to convert to progressive which how I usually create my DVDs anyway.
And while we're at it.. how about converting NTSC to PAL?
:)
evade
1st October 2004, 22:41
You aren't making dvds here if you are encoding mpeg4 :)
You can't losslessy convert an interlaced source to progressive, the best option is to deinterlace on playback with a filter and wait till mplayer g2 comes out and play your source back at its original 60fps. But if you insist on encoding at progressive you might apply a video filter like:
mencoder -vf pp=lb
(telecined video is another story altogether and can be undone pretty well)
For more filters rftm ;)
This results in dropped frames so you have to specify and ouput framerate.
mencoder -ofps 25 (PAL) or mencoder -ofps 24000/1001(NTSC)
That shoud do the framerate conversion you want to get the frame size right your command would look something like this.
mencoder -ofps 25 -vf pp=lb,scale=740:576 -sws 9
sws sets the software scaler 9=lanzcos default=bicubic
if you would like to encode for dvd you have at least 2 options with mencoder, use libavcodec's mpeg2 which is fast, supports 2-pass encoding and has a lot of options for tweaking quality but is not always dvd-compliant, or pipe to mpeg2enc from the mjpegtools.
I don't have a lot of experince encoding mpeg2 but your commands might look something like this, untested your milage may vary:
mencoder -ofps 25 -ovc lavc -oac lavc \
-lavcopts vcodec=mpeg2:acodec=mp2:abitrate=224:vbitrate=4500:MoreOptionsHere\
-vf pp=lb,scale=740:576,hqdn3d -sws 9 -of mpeg -o encoded.mpg \
source.avi
this won't be dvd compliant so you will still have to:
mplex -f8 -o dvd.mpg encoded.mpg
or
mplayer -vc dummy -vo null -ao pcm aofile temp.wav source.avi
toolame -s 48 temp.wav temp.mpa
mkfifo -m660 stream.yuv
mplayer -vf pp=lb,scale=720:576,hqdn3d -sws 9 -ac dummy -ao null -vo yuv4mpeg source.avi &
cat stream.yuv | Put processing tools here to convert framerate | mpeg2enc -f8 -F1 -a2 -o temp.m2v
mplex -f8 -o dvd.mpg temp.m2v temp.mpa
lots of info in the mplayer manual and on the mailing lists
http://mplayerhq.hu/pipermail/mplayer-users/
Or you could use transcode...
evade
2nd October 2004, 00:50
Originally posted by Huge
and skip videos that are already in the target format.
[/B]
To do this you could use -vc libdv,ffmpeg12 or the like in your mencoder command. It would error out if the source was not dv mpeg1 or mpeg2
Huge
3rd October 2004, 07:45
Originally posted by evade
Although the line you probably want to add is this:
if ($k =~ / 1 frames/) {$k="";}
Then it will only match on ' 1 frames' and not on '95541 frames'
I've added that line to the original script.
Here's another problemette... it seems that the original script can't cope with filenames with spaces or things like brackets in the filename. Just an FYI.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.