Difference between revisions of "Transcode"

From VideoLAN Wiki
Jump to navigation Jump to search
(Added a script example)
Line 43: Line 43:
  
 
{{doc|http://www.videolan.org/doc/streaming-howto/en/ch03.html}}
 
{{doc|http://www.videolan.org/doc/streaming-howto/en/ch03.html}}
 +
 +
As an example, here is a bash script that will take a <tt>.mpg</tt> file as generated by an DVB capture device and transcode it into an MPEG4 file using the h.264 codec. Copy it to a file, for example <tt>dvb2mp4</tt>, and make it executable. It should work on any operating system that has bash, including windows if [http://www.cygwin.com Cygwin] is installed.
 +
 +
<pre>
 +
#!/bin/bash
 +
#
 +
# Transcode DVB TV signals to h.264-encoded MP4 files using VLC
 +
# Wout.Mertens@gmail.com
 +
 +
#### Defaults, please edit for your settings ####
 +
 +
# VLC location, here is the Mac OS X example
 +
VLC=/Applications/VLC.app/Contents/MacOS/clivlc
 +
# Other possibilities: (Remove the # to activate)
 +
#VLC=/usr/bin/vlc
 +
#VLC=/usr/local/bin/vlc
 +
 +
# 16:9 or 4:3
 +
aspect=16:9
 +
 +
# PAL (Europe) or NTSC (US)
 +
src=PAL
 +
 +
# video bitrate
 +
bitrate=1024
 +
 +
# audio bitrate
 +
arate=128
 +
 +
# cropping?
 +
crop=yes
 +
 +
# Deinterlace?
 +
deint=yes
 +
 +
#### Program, do not change below this line ####
 +
usage() {
 +
cat 1>&2 <<EOF
 +
Usage: $0 [-swpncC] [-b <rate>] [-a <rate>] <DVB source .mpg> <MP4 output file>
 +
-s 4:3 aspect ratio (default=$aspect)
 +
-w 16:9 aspect ratio
 +
-p PAL (720x576) input (default=$src)
 +
-n NTSC (720x480) input
 +
-c Crop borders (default=$crop)
 +
-C Do not crop borders
 +
-d Perform de-interlacing (default=$deint)
 +
-D Do not perform de-interlacing
 +
-b video bitrate kb/s (default=$bitrate)
 +
-a audio bitrate kb/s (default=$arate)
 +
EOF
 +
exit 1
 +
}
 +
 +
error() {
 +
echo "ERROR: $*" 1>&2
 +
exit 2
 +
}
 +
 +
while getopts 'swpncCb:a:h' opt; do
 +
case $opt in
 +
s) aspect=4:3 ;;
 +
w) aspect=16:9 ;;
 +
p) src=PAL ;;
 +
n) src=NTSC ;;
 +
c) crop=yes ;;
 +
C) crop=no ;;
 +
d) deint=yes ;;
 +
D) deint=no ;;
 +
b) bitrate=$OPTARG ;;
 +
a) arate=$OPTARG ;;
 +
h) usage ;;
 +
*) echo "Unknown option $opt" 1>&2; usage ;;
 +
esac
 +
done
 +
shift $(( $OPTIND - 1 ))
 +
 +
infile="$1"
 +
outfile="$2"
 +
case $outfile in
 +
*.mp4|*.MP4) : ;;
 +
*) outfile="$outfile.mp4" ;;
 +
esac
 +
 +
[ -r "$infile" ] || error "Can't read from $infile"
 +
 +
case "$src,$aspect,$crop" in
 +
PAL,16:9,yes) sizestr="cropleft=31,cropright=31,croptop=16,cropbottom=16,width=960,height=544" ;;
 +
PAL,4:3,yes)  sizestr="cropleft=31,cropright=31,croptop=16,cropbottom=16,width=720,height=544" ;;
 +
NTSC,16:9,yes) sizestr="cropleft=30,cropright=30,croptop=17,cropbottom=17,width=800,height=448" ;;
 +
NTSC,4:3,yes) sizestr="cropleft=26,cropright=26,croptop=17,cropbottom=17,width=608,height=448" ;;
 +
PAL,16:9,no) sizestr="width=1024,height=576" ;;
 +
PAL,4:3,no) sizestr="width=768,height=576" ;;
 +
NTSC,16:9,no) sizestr="width=800,height=448" ;;
 +
NTSC,4:3,no) sizestr="width=608,height=448" ;;
 +
*) error "Could not handle $src,$aspect,$crop. This shouldn't happen!" ;;
 +
esac
 +
 +
if [ "$deint" = "yes" ]; then
 +
deintstr=",deinterlace=enable"
 +
else
 +
deintstr=
 +
fi
 +
 +
# Let's do it
 +
echo "Input file: $infile"
 +
echo "Output file: $outfile"
 +
echo "Encoding at $bitrate+$arate kb/s, input $src, $aspect, cropping $crop, de-interlace $deint"
 +
echo
 +
echo '>>>' $VLC -I dummy "$infile" --sout "#transcode{$sizestr$deintstr,vcodec=h264,vb=$bitrate,acodec=mp4a,ab=$arate}:standard{mux=mp4,dst=\"$outfile\",access=file}" vlc:quit
 +
echo
 +
$VLC -I dummy "$infile" --sout "#transcode{$sizestr$deintstr,vcodec=h264,vb=$bitrate,acodec=mp4a,ab=$arate}:standard{mux=mp4,dst=\"$outfile\",access=file}" vlc:quit
 +
</pre>

Revision as of 03:29, 27 December 2006

Transcoding is the process of taking a video file and changing it to a different format or bitrate.

Transcoding in VLC copies the movie to a new file in a different format, so you end up with both the original and new files. This means you need to have enough space on your hard drive to store this extra file. You should also have a reasonably fast computer, as transcoding can be very slow.

In VLC, transcoding is exactly the same as streaming accross a network, except that the output is sent to a file instead of a network.


Transcoding with the Wizard

VLC includes a transcoding and streaming wizard. To transcode a file, just select the transcode option. You will then be asked what format to convert to: you can give a video codec, and audio codec and an container format. Only some container formats can support some codecs - look here to see what supportes what.

For more information, see VideoLAN's Official Documentation

Transcoding with the Command Prompt

The most flexible way to transcode with vlc is using a command prompt to start vlc. Transcoding works the same as streaming. For example, the following command changes an asf file to an MPEG-2 file:

vlc "C:\Movies\Your File.asf" :sout='#transcode{vcodec=mp2v,vb=4096,acodec=mp2a,ab=192,scale=1,channels=2,deinterlace,audio-sync}:std{access=file, mux=ps,url="C:\Movies\Your File Output.ps.mpg"}'

The access=file instructs vlc to store the output in a file (and not stream it), and the url is the location of the new (output) file.

(Note: If you are running VLC on Mac OS X, you should use clivlc instead of VLC to avoid possible Bus error problems.)

Transcoding takes quite a while, so it's advisable to use an option like --stop-time=30 to only encode the first 30 seconds - this means you can check the file has transcoded correctly, and that the output is of a suitable quality.


The transcode statement can contain vcodec and vb to change the video codec and acodec and ab to change the audio codec. If vcodec is missing, the video codec will stay the same (same for acodec).


Common additional options to use are audio-sync (to make sure the audio is in sync correctly) and deinterlace (to increase quality slightly on interlaced video).

Under MS Windows

the syntax on windows is slightly different:

C:\Program Files\VideoLAN\VLC>vlc -vvv "D:\688497.flv" --sout=#transcode{vcodec= mp4v,acodec=mpga,vb=800,ab=128,deinterlace}:standard{access=file,mux=ts,url="D:\ asd.mpg"}

Completely non-interactive transcoding

For completely non-interactive transcoding (such as the case necessary when running under Mac OS X), the above example could be rewritten as

vlc -I dummy "C:\Movies\Your File.asf" :sout='#transcode{vcodec=mp2v,vb=4096,acodec=mp2a,ab=192,scale=1,channels=2,deinterlace,audio-sync}:std{access=file, mux=ps,url="C:\Movies\Your File Output.ps.mpg"}' vlc:quit

The two extra options are

-I dummy
Disables the graphical interface
vlc:quit
Quit VLC after transcoding

If transcoding from a DVD, it is also necessary to use the dvdsimple:// notation instead of the dvd:// notation, in order to avoid interacting with the DVD menu system (which is quite impossible without the graphical interface).

For more information, see VideoLAN's Official Documentation

As an example, here is a bash script that will take a .mpg file as generated by an DVB capture device and transcode it into an MPEG4 file using the h.264 codec. Copy it to a file, for example dvb2mp4, and make it executable. It should work on any operating system that has bash, including windows if Cygwin is installed.

#!/bin/bash
#
# Transcode DVB TV signals to h.264-encoded MP4 files using VLC
# Wout.Mertens@gmail.com

#### Defaults, please edit for your settings ####

# VLC location, here is the Mac OS X example
VLC=/Applications/VLC.app/Contents/MacOS/clivlc
# Other possibilities: (Remove the # to activate)
#VLC=/usr/bin/vlc
#VLC=/usr/local/bin/vlc

# 16:9 or 4:3
aspect=16:9

# PAL (Europe) or NTSC (US)
src=PAL

# video bitrate
bitrate=1024

# audio bitrate
arate=128

# cropping?
crop=yes

# Deinterlace?
deint=yes

#### Program, do not change below this line ####
usage() {
	cat 1>&2 <<EOF
Usage: $0 [-swpncC] [-b <rate>] [-a <rate>] <DVB source .mpg> <MP4 output file>
	-s	4:3 aspect ratio (default=$aspect)
	-w	16:9 aspect ratio
	-p	PAL (720x576) input (default=$src)
	-n	NTSC (720x480) input
	-c	Crop borders (default=$crop)
	-C	Do not crop borders
	-d	Perform de-interlacing (default=$deint)
	-D	Do not perform de-interlacing
	-b	video bitrate kb/s (default=$bitrate)
	-a	audio bitrate kb/s (default=$arate)
EOF
	exit 1
}

error() {
	echo "ERROR: $*" 1>&2
	exit 2
}

while getopts 'swpncCb:a:h' opt; do
	case $opt in
		s) aspect=4:3 ;;
		w) aspect=16:9 ;;
		p) src=PAL ;;
		n) src=NTSC ;;
		c) crop=yes ;;
		C) crop=no ;;
		d) deint=yes ;;
		D) deint=no ;;
		b) bitrate=$OPTARG ;;
		a) arate=$OPTARG ;;
		h) usage ;;
		*) echo "Unknown option $opt" 1>&2; usage ;;
	esac
done
shift $(( $OPTIND - 1 ))

infile="$1"
outfile="$2"
case $outfile in
	*.mp4|*.MP4) : ;;
	*) outfile="$outfile.mp4" ;;
esac

[ -r "$infile" ] || error "Can't read from $infile"

case "$src,$aspect,$crop" in
	PAL,16:9,yes) sizestr="cropleft=31,cropright=31,croptop=16,cropbottom=16,width=960,height=544" ;;
	PAL,4:3,yes)  sizestr="cropleft=31,cropright=31,croptop=16,cropbottom=16,width=720,height=544" ;;
	NTSC,16:9,yes) sizestr="cropleft=30,cropright=30,croptop=17,cropbottom=17,width=800,height=448" ;;
	NTSC,4:3,yes) sizestr="cropleft=26,cropright=26,croptop=17,cropbottom=17,width=608,height=448" ;;
	PAL,16:9,no) sizestr="width=1024,height=576" ;;
	PAL,4:3,no) sizestr="width=768,height=576" ;;
	NTSC,16:9,no) sizestr="width=800,height=448" ;;
	NTSC,4:3,no) sizestr="width=608,height=448" ;;
	*) error "Could not handle $src,$aspect,$crop. This shouldn't happen!" ;;
esac

if [ "$deint" = "yes" ]; then
	deintstr=",deinterlace=enable"
else
	deintstr=
fi

# Let's do it
echo "Input file: $infile"
echo "Output file: $outfile"
echo "Encoding at $bitrate+$arate kb/s, input $src, $aspect, cropping $crop, de-interlace $deint"
echo
echo '>>>' $VLC -I dummy "$infile" --sout "#transcode{$sizestr$deintstr,vcodec=h264,vb=$bitrate,acodec=mp4a,ab=$arate}:standard{mux=mp4,dst=\"$outfile\",access=file}" vlc:quit
echo
$VLC -I dummy "$infile" --sout "#transcode{$sizestr$deintstr,vcodec=h264,vb=$bitrate,acodec=mp4a,ab=$arate}:standard{mux=mp4,dst=\"$outfile\",access=file}" vlc:quit