]> code.delx.au - transcoding/blob - fix-pal-speedup
fix-pal-speedup now preserves surround sound for movies
[transcoding] / fix-pal-speedup
1 #!/bin/bash -e
2
3 # Many DVDs released in Australia are sped up from 24fps to 25fps.
4 # This script reverses the procedure, correcting the audio pitch.
5 # The video framerate is adjusted without re-encoding. The audio is
6 # slowed, and encoded as AAC, preserving surround sound.
7
8 if [ -z "$1" -o -z "$2" ]; then
9 echo "Usage: $0 destdir infile [infile ...]"
10 exit 1
11 fi
12
13 set -xe
14 FORCEFPS="24"
15 SLOWFILTER="-filter asetrate=46080,aresample=48000"
16
17 function mux_replace_audio {
18 local infile="$1"
19 local audiofile="$2"
20 local outfile="$3"
21
22 local trackid="$(mkvmerge -i "$infile" | grep 'Track ID.*video' | sed 's/^Track ID \(.\):.*$/\1/')"
23 mkvmerge -o "${outfile}" --default-duration "${trackid}:${FORCEFPS}fps" --no-audio "$infile" "$audiofile"
24 }
25
26 function extract_audio {
27 local infile="$1"
28 local outfile="$2"
29
30 mplayer \
31 -noconfig all \
32 -novideo \
33 -channels 8 \
34 -dumpaudio \
35 -dumpfile "$outfile" \
36 "$infile"
37 }
38
39 function encode_audio {
40 ffmpeg \
41 -i "$1" \
42 $SLOWFILTER \
43 -strict experimental \
44 "$2"
45 }
46
47 function convert_file {
48 local infile="$1"
49 local outfile="$2"
50 local audio1="${tmpdir}/audio1.ac3"
51 local audio2="${tmpdir}/audio2.m4a"
52
53 extract_audio "${infile}" "${audio1}"
54 encode_audio "${audio1}" "${audio2}"
55 mux_replace_audio "${infile}" "${audio2}" "${outfile}"
56 }
57
58
59 destdir="$1"
60 shift
61
62 for infile in "$@"; do
63 outfile="$destdir/$(basename "$infile")"
64
65 if [ -f "$outfile" ]; then
66 echo "Not overwriting $outfile"
67 continue
68 fi
69
70 tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")"
71 convert_file "$infile" "$outfile"
72 rm -rf "$tmpdir"
73 done
74