ffmpeg-mythtv/myth-rebuild-seektable.sh

110 lines
3.5 KiB
Bash
Raw Normal View History

#!/bin/bash
set -e
set -x
chanid=$1
starttime=$2
if test -z "$3"; then
workdir=${workdir:-"/tmp"}
else
workdir=$3
fi
fileroot="${chanid}_${starttime}"
ffprobe=${ffprobe:-"ionice -c 3 ffprobe"}
ffmpeg_to_mkv_seektable_awk=${ffmpeg_to_mkv_seektable_awk:-"/usr/local/bin/ffmpeg-to-mkv-seektable.awk"}
ffmpeg_to_mkv_marks_awk=${ffmpeg_to_mkv_marks_awk:-"/usr/local/bin/ffmpeg-to-mkv-marks.awk"}
# testing only
destdir=${destdir:-"/var/lib/mythtv/recordings"}
outfile=${outfile:-"$(ls $destdir/${fileroot}.{mpg,ts,mkv,mp4} 2>/dev/null | sort -u | head -n 1)"}
ending=$(echo "$outfile" | awk -v FS='.' '{print $NF}')
if test -z "$outfile"; then
echo No recording file found with root $fileroot and ending mpg, ts, mkv or mp4
exit 7
fi
if ! test -r "$outfile"; then
echo Recording file $outfile is not readable.
exit 8
else
outfile="$(basename $outfile)"
fi
if test -z "$ending"; then
echo Did not find file ending. Stop.
exit 9
fi
function get_keyframe_data {
key_frame_data=$workdir/${outfile}.2.txt
$ffprobe -v error -select_streams v:0 -show_entries frame=pict_type,coded_picture_number,pkt_pos,pkt_pts_time,interlaced_frame:side_data=nil -print_format default=noprint_wrappers=1 -i ${destdir}/${outfile} | grep --context=2 'pict_type=I' | awk -v ending="${ending}" -f $ffmpeg_to_mkv_seektable_awk > $key_frame_data
exitstatus=$?
if ! test $exitstatus -eq 0; then
exit $exitstatus
fi
if ! test -r $key_frame_data; then
exit 5
fi
}
function get_markup_data {
markup_data=$workdir/${outfile}.3.txt
$ffprobe -v error -show_format -show_streams -select_streams v:0 -show_entries stream=width,height,avg_frame_rate:format=duration -print_format default -i ${destdir}/${outfile} | awk -f $ffmpeg_to_mkv_marks_awk > $markup_data
exitstatus=$?
if ! test $exitstatus -eq 0; then
exit $exitstatus
fi
if ! test -r $markup_data; then
exit 5
fi
}
function set_markup_xml {
get_keyframe_data
get_markup_data
markup_xml=$workdir/${outfile}.xml
cat <<EOF> $markup_xml
<?xml version='1.0' encoding='utf-8'?>
<metadata>
<item>
<markup>
EOF
cat $key_frame_data $markup_data >> $markup_xml
cat <<EOF >> $markup_xml
</markup>
</item>
</metadata>
EOF
exitstatus=$?
if ! test $exitstatus -eq 0; then
exit $exitstatus
fi
if ! test -r $markup_data; then
exit 5
fi
}
function rebuild_seektable {
if test "$ending" = "mpg" -o "$ending" = "ts"; then
mythcommflag --chanid $chanid --starttime $starttime --rebuild
else
# file is mkv or mp4, so use ffprobe and mythutil
set_markup_xml && mythutil --quiet --chanid $chanid --starttime $starttime --setmarkup $markup_xml
# need to update filesize, too, since markup does not contain this
myth-update-basename.sh $chanid $starttime $outfile
fi
}
for t in skiplist cutlist seektable; do
mythutil --chanid $chanid --starttime $starttime --clear$t
done
rebuild_seektable
# function clear_seeks_from_recordedmarkup {
# markupfile="$workdir/$1.xml"
# mythutil --quiet --chanid $chanid --starttime $starttime --getmarkup $markupfile && sed -ri -e '/seek/{d}; /mark/{/type="3[0-2]|1[0-9]"/{p}; d};' $markupfile && mythutil --quiet --chanid $chanid --starttime $starttime --setmarkup $markupfile
# }
# function remove_start_prog {
# markupfile="$workdir/$1.xml"
# mythutil --quiet --chanid $chanid --starttime $starttime --getmarkup $markupfile && sed -ri -e '/type="40"/{d}' $markupfile && mythutil --quiet --chanid $chanid --starttime $starttime --setmarkup $markupfile
# }
exit $?