Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions comcut
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ if [ ! -f "$edlfile" ]; then
$comskipPath $comskipoutput --ini="$comskipini" "$infile"
fi

videocodec=$($ffmpegPath -hide_banner -nostdin -i "$infile" 2>&1 \
| grep "Video:" | sed 's/.*Video: //' | awk '{print $1}' | tr -d ',')

echo "Detected video codec: $videocodec"

ts_compatible=false
case "$videocodec" in
h264|hevc|mpeg2video|mpeg1video|mpeg4)
ts_compatible=true
;;
esac

if $ts_compatible; then
tempext="ts"
echo "Using MPEG-TS intermediate format"
else
tempext="mkv"
echo "Using MKV intermediate format (codec '$videocodec' not TS-compatible)"
fi

start=0
i=0
hascommercials=false
Expand Down Expand Up @@ -170,15 +190,17 @@ do
echo END=`echo "$end $totalcutduration" | awk '{printf "%i", ($1 - $2) * 1000}'` >> "$metafile"
echo "title=Chapter $i" >> "$metafile"

chapterfile="${infile%.*}.part-$i.ts"
chapterfile="${infile%.*}.part-$i.$tempext"

if [[ ! -z "$workdir" ]]; then
chapterfile=`basename "$chapterfile"`
chapterfile="$workdir$chapterfile"
fi

tempfiles+=("$chapterfile")
concat="$concat|$chapterfile"
if $ts_compatible; then
concat="$concat|$chapterfile"
fi

duration=`echo "$end" "$start" | awk '{printf "%f", $1 - $2}'`
$ffmpegPath -hide_banner -loglevel error -nostdin -i "$infile" -ss "$start" -t "$duration" -c copy -y "$chapterfile"
Expand All @@ -205,21 +227,33 @@ if $hascommercials ; then
echo END=`echo "$end $totalcutduration" | awk '{printf "%i", ($1 - $2) * 1000}'` >> "$metafile"
echo "title=Chapter $i" >> "$metafile"

chapterfile="${infile%.*}.part-$i.ts"
chapterfile="${infile%.*}.part-$i.$tempext"

if [[ ! -z "$workdir" ]]; then
chapterfile=`basename "$chapterfile"`
chapterfile="$workdir$chapterfile"
fi

tempfiles+=("$chapterfile")
concat="$concat|$chapterfile"
if $ts_compatible; then
concat="$concat|$chapterfile"
fi

duration=`echo "$end" "$start" | awk '{printf "%f", $1 - $2}'`
$ffmpegPath -hide_banner -loglevel error -nostdin -i "$infile" -ss "$start" -t "$duration" -c copy -y "$chapterfile"
fi

$ffmpegPath -hide_banner -loglevel error -nostdin -i "$metafile" -i "concat:${concat:1}" -c copy -map_metadata 0 -y "$outfile"
if $ts_compatible; then
$ffmpegPath -hide_banner -loglevel error -nostdin -i "$metafile" -i "concat:${concat:1}" -c copy -map_metadata 0 -y "$outfile"
else
filelistfile="${infile%.*}.filelist.txt"
printf "file '%s'\n" "${tempfiles[@]}" > "$filelistfile"
$ffmpegPath -hide_banner -loglevel error -nostdin \
-f concat -safe 0 -i "$filelistfile" \
-i "$metafile" \
-map 0 -map_metadata 1 -c copy -y "$outfile"
rm "$filelistfile"
fi
fi

for i in "${tempfiles[@]}"
Expand Down