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