]> code.delx.au - transcoding/blob - fix-pal-speedup
5b77d451163c122b638e618f181167fd0f675064
[transcoding] / fix-pal-speedup
1 #!/bin/bash
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 infile outfile"
10 exit 1
11 fi
12
13 set -o pipefail -eux
14 FORCEFPS="24"
15 SLOWFILTER="-filter asetrate=46080,aresample=osr=48000:resampler=soxr"
16
17 function mux_replace_audio {
18 local infile="$1"
19 local audiofile="$2"
20 local outfile="$3"
21
22 local audiodelay="$(get_minimum_timestamp "$infile" "audio")"
23 local videodelay="$(get_minimum_timestamp "$infile" "video")"
24 local videotrackid="$(get_track_id "$infile" "video")"
25
26 mkvmerge \
27 -o "${outfile}" \
28 --default-duration "${videotrackid}:${FORCEFPS}fps" \
29 --sync "${videotrackid}:$((videodelay / 1000000))" \
30 --no-audio "$infile" \
31 --sync "0:$((audiodelay / 1000000))" \
32 "$audiofile"
33 }
34
35 function get_track_id {
36 mkvmerge -i -F json "$1" | jq -r ".tracks[] | select(.type == \"$2\") | .id"
37 }
38
39 function get_minimum_timestamp {
40 mkvmerge -F json -i "$1" | jq -r ".tracks[] | select(.type == \"$2\") | .properties.minimum_timestamp"
41 }
42
43 function encode_audio {
44 ffmpeg \
45 -i "$1" \
46 -vn \
47 $SLOWFILTER \
48 -c:a libfdk_aac -vbr 3 \
49 "$2"
50 }
51
52 function convert_file {
53 local infile="$1"
54 local outfile="$2"
55 local audiofile="${tmpdir}/audiofile.m4a"
56
57 encode_audio "$infile" "$audiofile"
58 mux_replace_audio "$infile" "$audiofile" "$outfile"
59 }
60
61
62 infile="$1"
63 outfile="$2"
64 tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")"
65 convert_file "$infile" "$outfile"
66 rm -rf "$tmpdir"