]> code.delx.au - transcoding/blob - fix-pal-speedup
fix-pal-speedup switch back to mplayer
[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, volume normalised, down-mixed to stereo and encoded as mp3.
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 SLOWDOWN="0.96"
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 -ao "pcm:waveheader:file=${outfile}" \
34 "$infile"
35 }
36
37 function slow_audio {
38 local infile="$1"
39 local outfile="$2"
40
41 sox \
42 --temp "$tmpdir" \
43 "$infile" \
44 -t wav \
45 "$outfile" \
46 speed "${SLOWDOWN}" \
47 gain -n
48 }
49
50 function encode_audio {
51 local infile="$1"
52 local outfile="$2"
53
54 lame \
55 --preset standard \
56 "${infile}" "${outfile}"
57 }
58
59 function convert_file {
60 local infile="$1"
61 local outfile="$2"
62 local audio1="${tmpdir}/audio1.wav"
63 local audio2="${tmpdir}/audio2.wav"
64 local audio3="${tmpdir}/audio3.mp3"
65
66 extract_audio "${infile}" "${audio1}"
67 slow_audio "${audio1}" "${audio2}"
68 encode_audio "${audio2}" "${audio3}"
69 mux_replace_audio "${infile}" "${audio3}" "${outfile}"
70 }
71
72
73 destdir="$1"
74 shift
75
76 for infile in "$@"; do
77 outfile="$destdir/$(basename "$infile")"
78
79 if [ -f "$outfile" ]; then
80 echo "Not overwriting $outfile"
81 continue
82 fi
83
84 tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")"
85 convert_file "$infile" "$outfile"
86 rm -rf "$tmpdir"
87 done
88