]> code.delx.au - transcoding/blobdiff - fix-pal-speedup
fix-pal-speedup: don't try to adjust chapter-sync if there are zero chapters
[transcoding] / fix-pal-speedup
index 39c4ba4cbcbc5d2e46a73caffd9be3d41a1bda68..51ab6ed52fcacf4742c489eec58bc2cc458aaaf5 100755 (executable)
@@ -1,80 +1,90 @@
-#!/bin/bash -e
+#!/bin/bash
 
 # Many DVDs released in Australia are sped up from 24fps to 25fps.
 # This script reverses the procedure, correcting the audio pitch.
-# The video framerate is adjusted without re-encoding. The audio is
-# slowed, volume normalised, down-mixed to stereo and encoded as mp3.
+# - The video framerate is adjusted without re-encoding.
+# - The audio is slowed, and encoded as AAC, preserving surround sound.
+# - Chapters and subtitles are also adjusted to match the new timing.
 
-if [ -z "$1" -o -z "$2" ]; then
-    echo "Usage: $0 destdir infile [infile ...]"
+if [ -z "$1" ] || [ -z "$2" ]; then
+    echo "Usage: $0 infile outfile"
     exit 1
 fi
 
-set -xe
-FORCEFPS="24"
-SLOWDOWN="0.96"
+set -o pipefail -eux
+OLDFPS="25"
+NEWFPS="24"
+SLOWFILTER=("-filter" "asetrate=46080,aresample=osr=48000:resampler=soxr")
 
-function mux_replace_audio {
-    infile="$1"
-    audiofile="$2"
-    outfile="$3"
+function main {
+    local infile="$1"
+    local outfile="$2"
 
-    trackid="$(mkvmerge -i "$infile" | grep video | sed 's/^Track ID \(.\):.*$/\1/')"
-    mkvmerge -o "${outfile}" --default-duration "${trackid}:${FORCEFPS}fps" --no-audio "$infile" "$audiofile"
-}
-
-function extract_audio {
-    infile="$1"
+    local tmpdir=""
+    tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")"
+    local audiofile="${tmpdir}/audiofile.m4a"
 
-    exec mpv \
-        --no-terminal \
-        --no-video \
-        --ao pcm:waveheader:file=/dev/stdout \
-        "$infile"
-}
+    encode_audio "$infile" "$audiofile"
+    remux_file "$infile" "$audiofile" "$outfile"
 
-function slow_audio {
-    exec sox \
-        --temp "$tmpdir" \
-        - \
-        -t wav - \
-        speed "${SLOWDOWN}" \
-        gain -n \
-        channels 2
+    rm -rf "$tmpdir"
 }
 
 function encode_audio {
-    outfile="$1"
-    exec lame \
-        --preset standard \
-        - \
-        "${outfile}"
+    ffmpeg \
+        -i "$1" \
+        -vn \
+        "${SLOWFILTER[@]}" \
+        -c:a libfdk_aac -vbr 3 \
+        "$2"
 }
 
-function convert_file {
-    infile="$1"
-    outfile="$2"
-    tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")"
-    audiofile="${tmpdir}/audio.mp3"
-
-    extract_audio "${infile}" | slow_audio | encode_audio "${audiofile}"
-    mux_replace_audio "${infile}" "${audiofile}" "${outfile}"
+function remux_file {
+    local infile="$1"
+    local audiofile="$2"
+    local outfile="$3"
 
-    rm -rf "$tmpdir"
-}
+    local audiodelay=""
+    audiodelay="$(get_minimum_timestamp "$infile" "audio")"
 
+    local videodelay=""
+    videodelay="$(get_minimum_timestamp "$infile" "video")"
 
-destdir="$1"
-shift
+    local videotrackid=""
+    videotrackid="$(get_track_id "$infile" "video")"
 
-for infile in "$@"; do
-    outfile="$destdir/$(basename "$infile")"
+    local suboptions=()
+    local subtitletrackid=""
+    while read -r subtitletrackid; do
+        suboptions+=("--sync" "${subtitletrackid}:0,${OLDFPS}/${NEWFPS}")
+    done < <(get_track_id "$infile" "subtitles")
 
-    if [ -f "$outfile" ]; then
-        echo "Not overwriting $outfile"
-        continue
+    local chapteroptions=()
+    if [ "$(get_chapter_count "$infile")" -gt 0 ]; then
+        chapteroptions=("--chapter-sync" "0,${OLDFPS}/${NEWFPS}")
     fi
 
-    convert_file "$infile" "$outfile"
-done
+    mkvmerge \
+        -o "${outfile}" \
+        --default-duration "${videotrackid}:${NEWFPS}fps" \
+        --sync "${videotrackid}:$((videodelay / 1000000))" \
+        "${chapteroptions[@]}" \
+        "${suboptions[@]}" \
+        --no-audio "$infile" \
+        --sync "0:$((audiodelay / 1000000))" \
+        "$audiofile"
+}
+
+function get_track_id {
+    mkvmerge -i -F json "$1" | jq -r ".tracks[] | select(.type == \"$2\") | .id"
+}
+
+function get_minimum_timestamp {
+    mkvmerge -i -F json "$1" | jq -r ".tracks[] | select(.type == \"$2\") | .properties.minimum_timestamp"
+}
+
+function get_chapter_count {
+    mkvmerge -i -F json "$1" | jq -r ".chapters | length"
+}
 
+main "$@"