]> code.delx.au - transcoding/blob - fix-pal-speedup
Use '-' instead of /dev/{stdin,stdout}
[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 infile="$1"
19 audiofile="$2"
20 outfile="$3"
21
22 trackid="$(mkvmerge -i "$infile" | grep video | sed 's/^Track ID \(.\):.*$/\1/')"
23 exec mkvmerge -o "${outfile}" --default-duration "${trackid}:${FORCEFPS}fps" --no-audio "$infile" "$audiofile"
24 }
25
26 function extract_audio {
27 infile="$1"
28
29 exec mpv \
30 --no-terminal \
31 --no-video \
32 --ao pcm:waveheader:file=/dev/stdout \
33 "$infile"
34 }
35
36 function slow_audio {
37 exec sox \
38 --temp "$tmpdir" \
39 - \
40 -t wav - \
41 speed "${SLOWDOWN}" \
42 gain -n \
43 channels 2
44 }
45
46 function encode_audio {
47 outfile="$1"
48 exec lame \
49 --preset standard \
50 - \
51 "${outfile}"
52 }
53
54 function convert_file {
55 infile="$1"
56 outfile="$2"
57 tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")"
58 audiofile="${tmpdir}/audio.mp3"
59
60 extract_audio "${infile}" | slow_audio | encode_audio "${audiofile}"
61 mux_replace_audio "${infile}" "${audiofile}" "${outfile}"
62
63 rm -rf "$tmpdir"
64 }
65
66
67 destdir="$1"
68 shift
69
70 for infile in "$@"; do
71 outfile="$destdir/$(basename "$infile")"
72
73 if [ -f "$outfile" ]; then
74 echo "Not overwriting $outfile"
75 continue
76 fi
77
78 convert_file "$infile" "$outfile"
79 done
80