| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
|
By wapcaplet at 2004-06-07 23:16
|
|
If you've ever tried to find good software for creating VCD, SVCD, or DVD video discs in Linux, you may have met with some frustration, as I did. There are a lot of great video-conversion tools available, but most of them are run from the command-line with a bunch of arcane arguments. This is a little script I wrote that converts any video that can be played by mplayer into an appropriately-encoded mpeg for VCD, SVCD, or DVD. Parts of the script (in particular, the segment that extracts information from the video using tcprobe and perl) were stolen from the divx2cvcd script.
Running the script requires the following:
Here's the script. Save it as 'tovid' and be sure to chmod +x it so you can execute it.
Code:
#!/bin/bash
# Convert any video/audio stream that mplayer can play
# into VCD, SVCD, or DVD-compatible Mpeg output file.
# Arguments: $1 - format: Video CD, Super Video CD, DVD, or VCD-on-DVD [vcd|svcd|dvd]
# $2 - aspect: Widescreen or full-frame [wide|full]
# $3 - name of input file
# $4 - name of output prefix
USAGE="Usage: tovid [vcd|svcd|dvd|dvd-vcd] [wide|full] <input file> <output prefix>"
if [ $# -ne 4 ]; then
echo $USAGE
exit 1
elif [[ $1 == "vcd" || $1 == "dvd-vcd" ]]; then
TGTWIDTH="352"
TGTHEIGHT="240"
PALHEIGHT="288"
FORMAT="VCD"
VIDFMT="-f 1"
# For VCD, use VCD sound profile of mp2enc
if [[ $1 == "vcd" ]]; then
SNDOPTS="-V"
# For VCD-on-DVD, use DVD-format audio
else
SNDOPTS="-r 48000 -s"
fi
MUXOPTS="-m v"
SUF="m1v"
elif [[ $1 == "svcd" ]]; then
TGTWIDTH="480"
TGTHEIGHT="480"
PALHEIGHT="576"
FORMAT="SVCD"
# -d for dummy SVCD scan offsets
VIDFMT="-f 4 -d"
SNDOPTS="-V"
MUXOPTS="-m s"
SUF="m2v"
elif [[ $1 == "dvd" ]]; then
TGTWIDTH="720"
TGTHEIGHT="480"
PALHEIGHT="576"
FORMAT="DVD"
VIDFMT="-f 8"
SNDOPTS="-r 48000 -s"
MUXOPTS="-m d"
SUF="m2v"
else
echo $USAGE
exit 1
fi
if [[ $2 == "wide" ]]; then
ASPECT="WIDE"
elif [[ $2 == "full" ]]; then
ASPECT="FULL"
else
echo "$USAGE"
exit 1
fi
INFILE=$3
OUTPREFIX=$4
# Probe for width, height, and frame rate
tcprobe -i "$INFILE" > fileinfo
CURWIDTH=`grep 'import frame size' fileinfo | \
perl -e ' $line=<STDIN> ; $line =~ /import frame size: -g (\d+?)x\d+ / ; print $1' `
CURHEIGHT=`grep 'import frame size' fileinfo | \
perl -e ' $line=<STDIN> ; $line =~ /import frame size: -g \d+?x(\d+) / ; print $1' `
CURFPS=`grep 'frame rate' fileinfo | \
perl -e ' $line=<STDIN> ; $line =~ /frame rate: -f (.+?) \[/ ; print $1' `
echo "Input file is $CURWIDTH x $CURHEIGHT at $CURFPS fps."
# If FPS is already 29.97 (NTSC) or 23.976 (NTSC film), leave it alone
if [[ $CURFPS == "29.970" ]];
then
echo "Source is 29.970 fps (NTSC). Encoding as NTSC video."
ADJUSTFPS=""
VIDFPS="-F 4"
elif [[ $CURFPS == "23.976" ]];
then
echo "Source is 23.976 fps (NTSC film). Encoding as NTSC film."
ADJUSTFPS="yuvfps -s 24000:1001 -r 30000:1001 -v 0 |"
VIDFPS="-F 4"
else
echo "Source is not at an NTSC frame rate. Adjusting FPS."
ADJUSTFPS="yuvfps -r 30000:1001 -v 0 |"
VIDFPS="-F 4"
fi
# Set appropriate aspect ratio for output format
# Widescreen on DVD should be 16:9
[ $ASPECT == "WIDE" ] && [ $FORMAT == "DVD" ] && ASPECTFMT="-a 3"
# Widescreen on VCD/SVCD needs to be padded out to 4:3
[ $ASPECT == "WIDE" ] && [ $FORMAT != "DVD" ] && ASPECTFMT="-a 2"
# Standard (fullscreen) is always 4:3
[ $ASPECT == "FULL" ] && ASPECTFMT="-a 2"
# Estimate existing aspect ratio (integer math!)
ESTASPECT=$(($CURWIDTH * 100 / $CURHEIGHT))
# Tolerances for wide/full aspect ratio (+/- 10% of target)
if [[ $ASPECT == "WIDE" ]];
then
MINASPECT=160
MAXASPECT=195
else
MINASPECT=120
MAXASPECT=147
fi
# Determine whether any rescaling needs to be done
# If resolution is already the same as the target, do not rescale.
if [[ $CURWIDTH == $TGTWIDTH && $CURHEIGHT == $TGTHEIGHT ]];
then
echo "Source is already at target resolution ($TGTWIDTH x $TGTHEIGHT)."
echo "No rescaling will be applied."
ADJSIZE=""
# See if source is target resolution in PAL
# If so, just rescale; aspect ratio should be fine
elif [[ $CURWIDTH == $TGTWIDTH && $CURHEIGHT == $PALHEIGHT ]];
then
echo "Source appears to be PAL of target resolution ($TGTWIDTH x $PALHEIGHT)."
echo "Assuming correct aspect ratio and rescaling."
ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n |"
elif [[ $ESTASPECT -ge $MINASPECT && $ESTASPECT -le $MAXASPECT ]];
then
echo "Source is within 10% of target aspect ratio."
echo "Assuming correct aspect ratio and rescaling."
ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n |"
# Otherwise, scale and/or pad with black bars
else
echo "Scaling and/or padding with letterbox bars"
# For non-DVD formats, widescreen needs to be padded to make
# it fullscreen.
[ $FORMAT != "DVD" ] && [ $ASPECT == "WIDE" ] && \
ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n -M WIDE2STD |"
# Non-DVD standard sizes can be scaled directly
[ $FORMAT != "DVD" ] && [ $ASPECT == "FULL" ] && \
ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n |"
# DVD can be scaled directly
[ $FORMAT == "DVD" ] && \
ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n |"
fi
echo "Creating and encoding video stream..."
mkfifo stream.yuv
mplayer -nosound -noframedrop -vo yuv4mpeg -vf pp=hb/vb/dr,hqdn3d "$INFILE" &
eval `echo "cat stream.yuv | $ADJUSTFPS $ADJSIZE nice -n 16 mpeg2enc $ASPECTFMT $VIDFMT $VIDFPS -v 0 -n n -H -o $OUTPREFIX.$SUF"`
echo "Creating WAV of audio stream..."
mplayer -vc dummy -vo null -ao pcm -aofile stream.wav "$INFILE"
# echo "Normalizing WAV audio..."
# normalize --amplitude=-14dBFS stream.wav
echo "Encoding WAV..."
cat stream.wav | mp2enc $SNDOPTS -o "$OUTPREFIX.mpa"
echo "Multiplexing audio and video together..."
tcmplex -i "$OUTPREFIX.$SUF" -p "$OUTPREFIX.mpa" -o "$OUTPREFIX.mpg" $MUXOPTS
echo "Cleaning up..."
rm stream.yuv
rm stream.wav
# rm "$OUTPREFIX.$SUF"
# rm "$OUTPREFIX.mpa"
rm fileinfo
echo "Done"
To run the script, provide it with four arguments:
- The format you want to encode to (dvd, vcd, svcd, or dvd-vcd)
- The aspect ratio you want to use (widescreen or full-frame)
- The filename of the video you want to encode, e.g. Office_Space.avi
- The filename prefix of the output file you want., e.g. Office_Space_DVD
The widescreen or full-frame part is a little tricky, but really all you need to consider is whether the source video looks like it's in widescreen or in normal full-frame TV-style aspect ratio. That is, when you play the video, should it fill the screen, or should it have the black letterbox bars?
Caveat: This script only outputs NTSC format. If you are planning to play your video discs on a PAL-format television or something, this script will not help you (though you may be able to rewrite portions of it to make it do PAL).
I make no guarantees about this script. It probably still has some bugs, but you may find it useful.
|
|
|
|
All times are GMT -5. The time now is 01:53 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|
Thanks for the great article. I'd like to ask one small question.
I've used the SVCD version of the script (divx2svcd) to convert one DivX 5.1 file to the corresponding SVCD image, but got very irritating results.
First of all, the AVI length is something about 87000 frames (52 min.) and is 704 MB big. I got a dozen of files with sizes from 200 bytes to 900 MB big.
This is what I found in the folder containing the AVI file and the subtitle one.
The original files
the AVI file itself: MyDivXFilm.avi
the SUB file: MyDivXFilm.srt
The transcoded files
some file named MyDivXFilm.m2v and 720MB big
some file named MyDivXFilm.mpa and 67 MB big
some file named Pelicula.mpg and 900MB big
some file named Pelicula00.mpg and 712MB big
some file named Pelicula00.mpg.bin and 711MB big
some file named Pelicula00.mpg.cue and 178 Bytes big
some file named Pelicula01.mpg and 89MB big
some file named Pelicula01.mpg.bin and 87MB big
some file named Pelicula01.mpg.cue and 178 Bytes big
So, what is supposed to be done with these bunch of big&mighty files? And how comes it that I got the Film split in two parts?
One furter question: May I use a DVD media to write a (S)VCD where all parts of the film are in one place?
Many thanks in advance.
Kind regards,
sbogus
Though, I did realize after posting my script that there is one small mistake: there are two lines towards the end, under "Cleaning up..." that read:
I've made a lot of changes to my script since I posted this... I'll have to see about revising the posted version.
Todo:
Here it is. Enjoy!
#!/bin/bash # Convert any video/audio stream that mplayer can play # into VCD, SVCD, or DVD-compatible Mpeg output file. # Arguments: $1 - format: Video CD, Super Video CD, DVD, or VCD-on-DVD [vcd|svcd|dvd] # $2 - aspect: Widescreen or full-frame [wide|full] # $3 - name of input file # $4 - name of output prefix # Some constants to use: # Maximum bitrates (for video): # kbps guaranteed possible notes # ---------------------------------------------------- # 7500 81 min. 270 min. Good for full DVD # 3000 194 min. 400 min. Good for half-D1 DVD_BITRATE="7500" # Bitrate to use for Half-D1-encoded DVD format. Since resolution of Half-D1 is # a little less than half that of full DVD, bitrate can be half as much here with # quality (per pixel) comparable to full DVD. HALF_D1_BITRATE="3600" QUARTER_D1_BITRATE=$(($HALF_D1_BITRATE / 2)) USAGE="Usage: tovid [vcd|svcd|dvd|half-dvd|dvd-vcd] [wide|full|panavision] <input file> <output prefix>" if [ $# -ne 4 ]; then echo $USAGE exit 1 elif [[ $1 == "vcd" || $1 == "dvd-vcd" ]]; then TGTWIDTH="352" TGTHEIGHT="240" PALHEIGHT="288" # For VCD, use VCD sound profile of mp2enc if [[ $1 == "vcd" ]]; then FORMAT="VCD" VIDFMT="-f 1" SNDOPTS="-V" MUXOPTS="-m v" SUF="m1v" # For VCD-on-DVD, use DVD-format audio else FORMAT="SIZE_352x240" VIDFMT="-f 8 -b $QUARTER_D1_BITRATE" SNDOPTS="-r 48000 -s" MUXOPTS="-m d" SUF="m2v" fi elif [[ $1 == "svcd" ]]; then TGTWIDTH="480" TGTHEIGHT="480" PALHEIGHT="576" FORMAT="SVCD" # -d for dummy SVCD scan offsets VIDFMT="-f 4 -d" SNDOPTS="-V" MUXOPTS="-m s" SUF="m2v" elif [[ $1 == "dvd" ]]; then TGTWIDTH="720" TGTHEIGHT="480" PALHEIGHT="576" FORMAT="DVD" VIDFMT="-f 8 -b $DVD_BITRATE" SNDOPTS="-r 48000 -s" MUXOPTS="-m d" SUF="m2v" elif [[ $1 == "half-dvd" ]]; then TGTWIDTH="352" TGTHEIGHT="480" PALHEIGHT="576" FORMAT="SIZE_352x480" VIDFMT="-f 8 -b $HALF_D1_BITRATE" SNDOPTS="-r 48000 -s" MUXOPTS="-m d" SUF="m2v" else echo $USAGE exit 1 fi if [[ $2 == "wide" ]]; then ASPECT="WIDE" elif [[ $2 == "full" ]]; then ASPECT="FULL" elif [[ $2 == "panavision" ]]; then ASPECT="PANA" else echo "$USAGE" exit 1 fi INFILE=$3 OUTPREFIX=$4 # Filtering options to use with mplayer # pp=hb/vb/dr gives horizontal and vertical deblocking, plus deringing # hqdn3d gives high quality denoising VIDFILTER="-vf pp=hb/vb/dr,hqdn3d" # VIDFILTER="-vf hqdn3d" # Probe for width, height, and frame rate mplayer -vo null -ao null -frames 0 -identify "$INFILE" 2>/dev/null | \ grep "^ID_" > fileinfo CURWIDTH=`grep 'ID_VIDEO_WIDTH' fileinfo | \ sed -e 's/ID_VIDEO_WIDTH=//g'` CURHEIGHT=`grep 'ID_VIDEO_HEIGHT' fileinfo | \ sed -e 's/ID_VIDEO_HEIGHT=//g'` CURFPS=`grep 'ID_VIDEO_FPS' fileinfo | \ sed -e 's/ID_VIDEO_FPS=//g'` echo "Input file is $CURWIDTH x $CURHEIGHT at $CURFPS fps." # If FPS is already 29.97 (NTSC) or 23.976 (NTSC film), leave it alone if [[ $CURFPS == "29.970" ]]; then echo "Source is 29.970 fps (NTSC). Encoding as NTSC video at 29.97 FPS." ADJUSTFPS="" VIDFPS="-F 4" elif [[ $CURFPS == "23.976" ]]; then echo "Source is 23.976 fps (NTSC film). Adjusting FPS to 29.97." ADJUSTFPS="yuvfps -s 24000:1001 -r 30000:1001 -v 0 |" VIDFPS="-F 4" else echo "Source is not at an NTSC frame rate. Adjusting FPS to 29.97." ADJUSTFPS="yuvfps -r 30000:1001 -v 0 |" VIDFPS="-F 4" fi # Set appropriate aspect ratio for output format # Widescreen on DVD should be 16:9 [ $ASPECT == "WIDE" ] && [ $FORMAT == "DVD" ] && ASPECTFMT="-a 3" # Panavision on DVD is encoded at 16:9 with letterboxing added [ $ASPECT == "PANA" ] && [ $FORMAT == "DVD" ] && ASPECTFMT="-a 3" # Widescreen on VCD/SVCD needs to be padded out to 4:3 [ $ASPECT == "WIDE" ] && [ $FORMAT != "DVD" ] && ASPECTFMT="-a 2" # Standard (fullscreen) is always 4:3 [ $ASPECT == "FULL" ] && ASPECTFMT="-a 2" # Estimate existing aspect ratio (integer math!) ESTASPECT=$(($CURWIDTH * 100 / $CURHEIGHT)) # Tolerances for wide/full aspect ratio (+/- 10% of target) if [[ $ASPECT == "WIDE" ]]; then MINASPECT=160 MAXASPECT=195 else MINASPECT=120 MAXASPECT=147 fi # Determine whether any rescaling needs to be done # If resolution is already the same as the target, do not rescale. if [[ $CURWIDTH == $TGTWIDTH && $CURHEIGHT == $TGTHEIGHT ]]; then echo "Source is already at target resolution ($TGTWIDTH x $TGTHEIGHT)." echo "No rescaling will be applied." ADJSIZE="" # See if source is target resolution in PAL # If so, just rescale; aspect ratio should be fine elif [[ $CURWIDTH == $TGTWIDTH && $CURHEIGHT == $PALHEIGHT ]]; then echo "Source appears to be PAL of target resolution ($TGTWIDTH x $PALHEIGHT)." echo "Assuming correct aspect ratio and rescaling." ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n |" elif [[ $ESTASPECT -ge $MINASPECT && $ESTASPECT -le $MAXASPECT ]]; then echo "Source is within 10% of target aspect ratio." echo "Assuming correct aspect ratio and rescaling." ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n |" # Otherwise, scale and/or pad with black bars else echo "Scaling and/or padding with letterbox bars" # For non-DVD formats, widescreen needs to be padded to make # it fullscreen. if [ $FORMAT != "DVD" ] && [ $ASPECT == "WIDE" ]; then echo "Letterboxing widescreen aspect to fit in 4:3 aspect" ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n -M WIDE2STD |" # Non-DVD standard sizes can be scaled directly elif [ $FORMAT != "DVD" ] && [ $ASPECT == "FULL" ]; then echo "Scaling full-screen aspect" ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n |" # Non-panavision DVD can be scaled directly elif [ $ASPECT != "PANA" ] && [ $FORMAT == "DVD" ]; then "Scaling directly to DVD resolution" ADJSIZE="yuvscaler -O $FORMAT -v 0 -n n |" # Panavision (2.35:1) DVD must be padded within the 16:9 frame elif [ $ASPECT == "PANA" ] && [ $FORMAT == "DVD" ]; then echo "Letterboxing Panavision (2.35:1) aspect to fit DVD widescreen aspect" ADJSIZE="yuvscaler -O DVD -v 0 -n n |" VIDFILTER=`echo $VIDFILTER,scale=720:363,expand=720:480` fi fi echo "Creating and encoding video stream..." mkfifo stream.yuv mplayer -nosound -noframedrop -vo yuv4mpeg $VIDFILTER "$INFILE" & eval `echo "cat stream.yuv | $ADJUSTFPS $ADJSIZE nice -n 16 mpeg2enc $ASPECTFMT $VIDFMT $VIDFPS -v 0 -n n -H -o $OUTPREFIX.$SUF"` echo "Creating WAV of audio stream..." mplayer -vc dummy -vo null -ao pcm -aofile stream.wav "$INFILE" # echo "Normalizing WAV audio..." # normalize --amplitude=-14dBFS stream.wav echo "Encoding WAV..." cat stream.wav | mp2enc $SNDOPTS -o "$OUTPREFIX.mpa" echo "Multiplexing audio and video together..." tcmplex -i "$OUTPREFIX.$SUF" -p "$OUTPREFIX.mpa" -o "$OUTPREFIX.mpg" $MUXOPTS echo "Cleaning up..." rm stream.yuv rm stream.wav rm "$OUTPREFIX.$SUF" rm "$OUTPREFIX.mpa" rm fileinfo echo "Done"Rather than continue to clutter the forum with updates to this script, I thought I'd just direct your attention to the tovid homepage and tovid project summary page on Sourceforge. Patches and updates are welcome!
But the homesite and the download section does not work - I can't download the 0.1 version of the script&GUI. Am I doing smoething wrong or because the project is very new to SF, there's not yet mirrored files to download?
Kind regards,
sbogus
But the homesite and the download section does not work - I can't download the 0.1 version of the script&GUI.
now it worked with the download.
What version of mplayer do you use with the script? Because mine is 1.0.pre4[whatever] and keeps instantly crashing when the script tries to extract the audio from the AVI file. The file itself plays perfect in the player but extracting the audio does not work.
I've also played with the options, but could not manage to tell mplayer to extract the audio only using other options than the ones you've used in the script. But then again, it crashes with these options...
Thanks in advance.
Kind regards,
sbogus
it segfaults (crashed with signal 11).
It crashes on each AVI file that has MP3 audio therein (XViD, DivX 5.x)) and unfortunately all my films&videos are either in XViD or in DivX 5.x format.
The MP3 audio stream is always in 44100 Hz frequency, the codec that is used to play the stream is "lame", but sometimes it uses also "MAD" for the playing. I don't know why...
Thanks for helping me continuously.
Kind regards,
sbogus
Since I am unable to get any of my AVIs to cause crashing, I'd like to use you as a guinea-pig for another little experiment, if you don't mind. Try this command:
Another thing to try, if you haven't already, is upgrading mplayer and/or lame. Could just be a bug with one of those.
Thanks for all your feedback so far! You're my first beta-tester