Scripts

One off scripting commands and small QOL improvement scripts not needing their own repository.

Bash script to require a specific keypress or die.
echo 'This will cut a production release by overwriting prod with dev.'
read -n 1 -p 'Press Y to continue, any other key to abort: ' READ_CONTINUE

if [ "${READ_CONTINUE}" != 'Y' ]; then
  echo -e '\nAborting.'
  exit 1
fi

fflength determines the length of a video file.

0700 user user fflength
1#!/bin/bash
2#
3# Determines overall length in seconds of a given video file.
4
5ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$1" | cut -d\. -f1

playon-to-mkv converts playon videos to MKV containers.

0700 user user playon-to-mkv
 1#!/bin/bash
 2#
 3# Converts playon videos to mkv format, preserving encoding and stripping metadata.
 4# For multiple: find -type f -exec ~/bin/playon-to-mkv netflix|amazon|playon|custom {}
 5#
 6
 7# playon-to-mkv [netflix|amazon|playon] file
 8# playon-to-mkv custom pre-trim post-trim file
 9
10# Playon adds a 4 second pre-roll video with account/IP address, strip this.
11# Playon adds a 5 second post-roll video with account/IP address, strip this.
12# Playon adds metadata pertaining to account for video, strip this.
13PLAYON_PRE=4
14PLAYON_POST=5
15
16# Netflix adds a 6.5 second netflix video, strip this.
17# Netflix can inject a blank screen at the end, but it is not consistent.
18NETFLIX_PRE=6
19NETFLIX_POST=0
20
21# Amazon adds a 1.5 second post-roll video for series, strip this.
22AMAZON_PRE=0
23AMAZON_POST=1.5
24
25echo 'Calculating distances ...'
26FILE="$2"
27case "$1" in
28  "netflix")
29    echo 'Processing NETFLIX'
30    PRE=$(awk '{print $1+$2}' <<< "${PLAYON_PRE} ${NETFLIX_PRE}")
31    POST=$(awk '{print $1+$2}' <<< "${PLAYON_POST} ${NETFLIX_POST}")
32    ;;
33  "amazon")
34    echo 'Processing AMAZON'
35    PRE=$(awk '{print $1+$2}' <<< "${PLAYON_PRE} ${AMAZON_PRE}")
36    POST=$(awk '{print $1+$2}' <<< "${PLAYON_POST} ${AMAZON_POST}")
37    ;;
38  "custom")
39    echo 'Processing CUSTOM'
40    PRE=$2
41    POST=$3
42    FILE="$4"
43    ;;
44  *)
45    echo 'Prcessing PLAYON'
46    PRE=${PLAYON_PRE}
47    POST=${PLAYON_POST}
48    ;;
49esac
50filename=$(basename "${FILE}")
51dir=$(dirname "${FILE}")
52extension="${filename##*.}"
53basename="${filename%.*}"
54
55ORIGINAL_LENGTH=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${FILE}" | cut -d\. -f1)
56TRIM_LENGTH=$(awk '{print $1-$2-$3}' <<< "${ORIGINAL_LENGTH} ${PRE} ${POST}")
57echo -e "Distances: \nLength: ${ORIGINAL_LENGTH}\nPre Offset: ${PRE}\nPost Offset: ${POST}\nTrim: ${TRIM_LENGTH}"
58
59echo 'Stripping metadata and trimming ...'
60ffmpeg -i "${FILE}" -ss ${PRE} -acodec copy -vcodec copy -map_metadata -1 -t ${TRIM_LENGTH} "${dir}/${basename}.stripped.${extension}"
61echo 'Packing into mkv ...'
62mkvmerge -o "$-{basename}.mkv" "${basename}.stripped.${extension}"
63echo 'Setting media permssions ...'
64chown ${USER}:${USER} "${basename}.mkv"
65chmod 0640 "${basename}.mkv"