Skip to content

Instantly share code, notes, and snippets.

@Aikhjarto
Created May 27, 2017 11:40
Show Gist options
  • Save Aikhjarto/8182317e31c86007296caa643e894841 to your computer and use it in GitHub Desktop.
Save Aikhjarto/8182317e31c86007296caa643e894841 to your computer and use it in GitHub Desktop.
Automatically demux VDR recordings
#!/bin/bash
# This script searched for *.ts files recursivly in $SRC_DIR. Then it demuxes the files with ProjectX to $DST_DIR whilest maintaining the folder structure.
# This us useful if mpeg files are post-processed with ttcutter or cuttermaran
# TODO: projectx.sh doesn't work with spaces in filenames. I found no way to espace them to make it work.
# Thomas Wagner 2014 wagner-thomas@gmx.at
# for loop only breaks at linefeed (otherwise space in filename won't work)
IFS=$'\012'
SRC_DIR="/var/spool/vdr"
DST_DIR="/scratch/vdr_demux"
set -o errexit
set -o nounset
set -o pipefail
# remove recordings (folders matching *.rec) from $DST_DIR that are no longer in $SRC_DIR
# catch error if $SRC_DIR is not readable (would purge output completely)
find "$SRC_DIR" -type d -name "*.rec" -printf '%P\n' | sort > "$DST_DIR"/src_recs
find "$DST_DIR" -type d -name "*.rec" -printf '%P\n' | sort > "$DST_DIR"/dst_recs
# this is necessary because errexit won't stop this script when error in "find" occur as stdin of comm
pushd "$DST_DIR"
echo "recordings purged from $DST_DIR"
comm -23 dst_recs src_recs
comm -23 dst_recs src_recs | xargs rm -rf
echo "done purging"
popd
# remove empty folders (Timers with no recordings) [doesnt' work right now] error is find: ‘/scratch/vdr_demux/bla’: No such file or directory
echo "purge empty dirs"
sync
find "$DST_DIR" -type d -empty
find "$DST_DIR" -type d -empty -exec rmdir {} \;
echo "done purging"
# loop over all recordings (folder that match *.rec)
# sort is just for style. Ensures processing is done in alphabetical order.
for REC in $(find "$SRC_DIR" -type d -name "*.rec" -printf '%P\n' | grep -v \#copied | sort);
do
# $REC is something like test/2015-01-24.10.16.15-0.rec
# define output folder
OUT_DIR="$DST_DIR/$REC"
# log file is the last file to be written, if it's there, demuxing is finished
if [ -f "$OUT_DIR"/00001_log.txt ]; then
echo "$REC was already processed"
else
# TODO check if vdr is still writing
# svdrpsend NEXT rel return 250 15 -23, where 250 if success status, 15 the timer number and -23 the relative time in seconds. If rel time is negative, a record 15 is running.
if [ -z $(svdrpsend NEXT rel | grep 250 | awk '{ print $3 }' | grep "-") ]; then
# check if stream is an mpeg2 stream
if ffprobe -i "$SRC_DIR/$REC/00001.ts" 2>&1 | grep Stream | grep -q mpeg2; then
echo "Processing $REC to $OUT_DIR"
if [ -d "$OUT_DIR" ]; then
# purge directory if it already exists
rm -rf "$OUT_DIR"
fi
mkdir -p "$OUT_DIR"
# start projectx
nice -n 9 projectx.sh -log -demux -ini "$SRC_DIR"/ProjectX.ini -out "$OUT_DIR" "$SRC_DIR/$REC"/*.ts 2>&1 > "$OUT_DIR"/stdout
else
echo "$REC does not contain an mpeg2 stream"
fi
else
echo "Skip demuxing $REC, VDR is recording"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment