]> code.delx.au - transcoding/blob - fix-pal-speedup
67397c09a742f5d7c32d282a4ebc99ffd746bafa
[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 mpv \
31 --no-terminal \
32 --no-video \
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 channels 2
49 }
50
51 function encode_audio {
52 local infile="$1"
53 local outfile="$2"
54
55 lame \
56 --preset standard \
57 "${infile}" "${outfile}"
58 }
59
60 function convert_file {
61 local infile="$1"
62 local outfile="$2"
63 local audio1="${tmpdir}/audio1.wav"
64 local audio2="${tmpdir}/audio2.wav"
65 local audio3="${tmpdir}/audio3.mp3"
66
67 extract_audio "${infile}" "${audio1}"
68 slow_audio "${audio1}" "${audio2}"
69 encode_audio "${audio2}" "${audio3}"
70 mux_replace_audio "${infile}" "${audio3}" "${outfile}"
71 }
72
73
74 destdir="$1"
75 shift
76
77 for infile in "$@"; do
78 outfile="$destdir/$(basename "$infile")"
79
80 if [ -f "$outfile" ]; then
81 echo "Not overwriting $outfile"
82 continue
83 fi
84
85 tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")"
86 convert_file "$infile" "$outfile"
87 rm -rf "$tmpdir"
88 done
89