Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daald/50095e8595d1e5219013f0498a000ba3 to your computer and use it in GitHub Desktop.
Save daald/50095e8595d1e5219013f0498a000ba3 to your computer and use it in GitHub Desktop.
Script for scaling down HD movie after recording using TVheadend to a DVBcut compatible SD movie for archiving
#!/bin/sh
set -x -e
q=3.5
# q3.4 = 324354 bytes/s
# q3.5 = 251931 bytes/s -> best choice
infile="$1" # testrec-hd.ts
outfile="${1%.ts}.mpeg" # testrec-sd-q$q.mpeg"
outfiletmp="${1%.ts}.~part~.mpeg" # testrec-sd-q$q.part.mpeg"
panic() {
echo "$0: $1" >&2
exit 1
}
[ -f "$infile" ] || panic "infile $infile does not exist"
[ ! -f "$outfile" ] || panic "outfile $outfile already exists"
tmpfile=/tmp/$$.tmp
rm -fv "$outfiletmp" "$tmpfile"
ffmpeg -i "$infile" >$tmpfile 2>&1 || true
cat "$tmpfile"
grep -q "Video: h264" "$tmpfile" || panic "Unexpected format of $infile"
grep -q "Input #0, mpegts, " "$tmpfile" || panic "Unexpected format of $infile"
case "$(cat "$tmpfile" | grep ': Video: ' | grep -Po ' [0-9]+x[0-9]+ ')" in
' 1280x720 ') dims=720x576;; # factor 1:0.5625
*)
panic "Unable to detect source dimensions"
esac
if ffmpeg -i "$infile" -map 0:v -map 0:a:0 -map 0:a:1 -f mpegts -s "$dims" -r 25 -vcodec mpeg2video -qscale:v $q -acodec copy "$outfiletmp"; then
touch --reference "$infile" "$outfiletmp"
mv -v "$outfiletmp" "$outfile"
echo "ok."
else
r=$?
rm -fv "$outfiletmp"
exit $r
fi
# HD Reference file
#
# Input #0, mpegts, from 'testrec-hd.ts':
# Duration: 00:10:30.42, start: 44092.375256, bitrate: 11724 kb/s
# Program 9038
# Stream #0:0[0x50]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 50 fps, 50 tbr, 90k tbn, 100 tbc
# Stream #0:1[0x51](ger): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, stereo, s16p, 192 kb/s
# Stream #0:2[0x52](eng): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, stereo, s16p, 192 kb/s
# Stream #0:3[0x5b](ger): Audio: ac3 ([129][0][0][0] / 0x0081), 48000 Hz, 5.1(side), fltp, 448 kb/s
# No Program
# Stream #0:4[0x6e]: Audio: mp3, 0 channels, s16p
# At least one output file must be specified
# HD downscaled q=3.5
#
# Input #0, mpegts, from 'out-sd-q3.5.mpeg':
# Duration: 00:10:30.48, start: 1.400000, bitrate: 2015 kb/s
# Program 1
# Stream #0:0[0x100]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 64:45 DAR 16:9], max. 104857 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
# Stream #0:1[0x101](ger): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 192 kb/s
# Stream #0:2[0x102](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 192 kb/s
# At least one output file must be specified
# SD Reference file
#
# Input #0, mpegts, from 'testrec-sd.mpeg':
# Duration: 00:10:30.06, start: 45811.850811, bitrate: 2390 kb/s
# Program 1
# Stream #0:0[0xa0]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 64:45 DAR 16:9], max. 6000 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
# Stream #0:1[0x50](ger): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, stereo, s16p, 128 kb/s
# Stream #0:2[0x51](eng): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, mono, s16p, 64 kb/s
# No Program
# Stream #0:3[0x20]: Audio: mp3, 0 channels, s16p
# At least one output file must be specified
# name size time s b b/s fazit
# testrec-hd-reference.ts hd 00:10:30.42 630.42 923901184 1465532.79
# testrec-sd-reference.mpeg sd 00:10:30.06 630.06 188298356 298857.82
# testrec-sd-q1.mpeg sd 00:10:30.42 630.42 347443364 551129.98
# testrec-sd-q2.mpeg sd 00:10:30.42 630.42 346557696 549725.10
# testrec-sd-q2.4.mpeg sd 00:10:30.42 630.42 343700284 545192.54
# testrec-sd-q3.mpeg sd 00:10:30.42 630.42 204833332 324915.66 ok
# testrec-sd-q3.1.mpeg sd 00:10:30.42 630.42 204833332 324915.66
# testrec-sd-q3.2.mpeg sd 00:10:30.42 630.42 204833332 324915.66
# testrec-sd-q3.3.mpeg sd 00:10:30.42 630.42 204479328 324354.13
# testrec-sd-q3.4.mpeg sd 00:10:30.42 630.42 204479328 324354.13
# testrec-sd-q3.5.mpeg sd 00:10:30.42 630.42 158822588 251931.39 best with small size
# testrec-sd-q3.6.mpeg sd 00:10:30.42 630.42 158822588 251931.39
# testrec-sd-q3.7.mpeg sd 00:10:30.42 630.42 158809240 251910.22
# testrec-sd-q3.8.mpeg sd 00:10:30.42 630.42 159331504 252738.66
# testrec-sd-q4.mpeg sd 00:10:30.42 630.42 159331504 252738.66 usable
# testrec-sd-q5.mpeg sd 00:10:30.42 630.42 132361400 209957.49 bad
# testrec-sd-q6.mpeg sd 00:10:30.42 630.42 116979992 185558.82
# testrec-sd-q7.mpeg sd 00:10:30.42 630.42 106930640 169618.10
# testrec-sd-q8.mpeg sd 00:10:30.42 630.42 99986860 158603.57
# testrec-sd-q9.mpeg sd 00:10:30.42 630.42 94175216 149384.88
# testrec-sd-q10.mpeg sd 00:10:30.42 630.42 89957248 142694.15
# testrec-sd-q11.mpeg sd 00:10:30.42 630.42 86698832 137525.51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment