Giter Site home page Giter Site logo

Comments (3)

BrettSheleski avatar BrettSheleski commented on July 20, 2024

I would rather keep these tools as standalone tools that do one job only.

What would make sense to me is instead of writing the resulting video to a file, instead provide a way to output to stdout. Therefore it could be piped in to other tools like handbrake or whatever you want.

I am curious how you did what you did though.

Perhaps you could update the readme.md file as a usage example instead.

from comchap.

nambrosch avatar nambrosch commented on July 20, 2024

this is the first time i've touched plex post-processing so i'm not familiar with the capabilities of any tools that people normally use. if using a pipe is more efficient then i could look into that.

here's what i have so far, i did a little cleanup (sorry) and added the handbrake bits in there. i'll test it with plex tomorrow but i forsee it working fine.

to test:

$ time sh ./comcut-new --use-handbrake video_file.ts video_file_processed.mp4

and the script:

#!/usr/bin/env bash

# LD_LIBRARY_PATH is set and will mess up ffmpeg, unset it, then re-set it when done
ldPath=${LD_LIBRARY_PATH}
unset LD_LIBRARY_PATH

exitcode=0

comskipPath="comskip"
ffmpegPath="ffmpeg"
handbrakePath="HandBrakeCLI"

if [[ $# -lt 1 ]]; then
  exename=$(basename "$0")

  echo "Remove commercial from video file using EDL file"
  echo "     (If no EDL file is found, comskip will be used to generate one)"
  echo ""
  echo "Usage: $exename infile [outfile]"

  exit 1
fi

comskipini=$HOME/.comskip.ini

deleteedl=true
deletelog=true
deletelogo=true
deletemeta=true
deletetxt=true
lockfile=""
usehandbrake=false
workdir=""

while [[ $# -gt 1 ]]; do
  key=$1
  case $key in
    --comskip=*)
      comskipPath="${key#*=}"
      shift
      ;;
    --comskip-ini=*)
      comskipini="${key#*=}"
      shift
      ;;
    --ffmpeg=*)
      ffmpegPath="${key#*=}"
      shift
      ;;
    --handbrake=*)
      handbrakePath="${key#*=}"
      shift
      ;;
    --keep-edl)
      deleteedl=false
      shift
      ;;
    --keep-meta)
      deletemeta=false
      shift
      ;;
    --lockfile=*)
      lockfile="${key#*=}"
      shift
      ;;
    --use-handbrake)
      usehandbrake=true
      shift
      ;;
    --work-dir=*)
      workdir="${key#*=}"
      shift
      ;;
    *)
      break
      ;;
  esac
done

# make sure comskip exists
if [[ ! $(type "$comskipPath") ]]; then
  "error: ${comskipPath} does not exist, exiting"
  exit 1
fi

# make sure ffmpeg exists
if [[ ! $(type "$ffmpegPath") ]]; then
  "error: ${ffmpegPath} does not exist, exiting"
  exit 1
fi

# make sure handbrakecli exists (if enabled)
if [[ ! $(type "$handbrakePath") && $usehandbrake ]]; then
  "error: ${handbrakePath} is not executable, exiting"
  exit 1
fi

if [[ ! -z "$lockfile" ]]; then
  echo "lockfile: $lockfile"
  while [[ -f "$lockfile" ]]; do
    echo "Waiting"
    sleep 5
  done

  touch "$lockfile"
fi

if [[ ! -f "$comskipini" ]]; then
  echo "output_edl=1" > "$comskipini"
elif [[ ! $(grep -q "output_edl=1" "$comskipini") ]]; then
  echo "output_edl=1" >> "$comskipini"
fi

infile=$1
outfile=$infile

if [[ -z "$2" ]]; then
  outfile="$infile"
else
  outfile="$2"
fi

outdir=$(dirname "$outfile")

outextension="${outfile##*.}"
comskipoutput=""

if [[ ! -z "$workdir" ]]; then
  case "$workdir" in
    */)
      ;;
    *)
      comskipoutput="--output=$workdir"
      workdir="$workdir/"
      ;;
  esac
fi

edlfile="$workdir${infile%.*}.edl"
metafile="$workdir${infile%.*}.ffmeta"
logfile="$workdir${infile%.*}.log"
logofile="$workdir${infile%.*}.logo.txt"
txtfile="$workdir${infile%.*}.txt"

if [[ ! -f "$edlfile" ]]; then
  $comskipPath $comskipoutput --ini="$comskipini" "$infile"
fi

start=0
i=0
hascommercials=false

concat=""

tempfiles=()
totalcutduration=0

echo ";FFMETADATA1" > "$metafile"

# Reads in from $edlfile, see end of loop.
while IFS=$'\t' read -r -a line; do
  end="${line[0]}"
  startnext="${line[1]}"

  if [ `echo "$end" | awk '{printf "%i", $0 * 1000}'` -gt `echo "$start" | awk '{printf "%i", $0 * 1000}'` ]; then
    ((i++))

    hascommercials=true

    echo [CHAPTER] >> "$metafile"
    echo TIMEBASE=1/1000 >> "$metafile"
    echo START=`echo "$start  $totalcutduration" | awk  '{printf "%i", ($1 - $2) * 1000}'` >> "$metafile"
    echo END=`echo "$end $totalcutduration" | awk  '{printf "%i", ($1 - $2) * 1000}'` >> "$metafile"
    echo "title=Chapter $i" >> "$metafile"

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

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

    tempfiles+=("$chapterfile")
    concat="$concat|$chapterfile"

    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"

    totalcutduration=`echo "$totalcutduration" "$startnext" "$end" | awk  '{print $1 + $2 - $3}'`

  fi

  start=$startnext
done < "$edlfile"

if $hascommercials ; then

  #dont forget to add the final part from last commercial to end of file
  end=`$ffmpegPath -hide_banner -nostdin -i "$infile" 2>&1 | grep Duration | awk '{print $2}' | tr -d , | awk -F: '{ printf "%f", ($1*3600)+($2*60)+$3 }'`

  if [ `echo "$end" | awk '{printf "%i", $0 * 1000}'` -gt `echo "$start" | awk '{printf "%i", $0 * 1000}'` ]; then

    ((i++))

    echo [CHAPTER] >> "$metafile"
    echo TIMEBASE=1/1000 >> "$metafile"
    echo START=`echo "$start  $totalcutduration" | awk  '{printf "%i", ($1 - $2) * 1000}'` >> "$metafile"
    echo END=`echo "$end $totalcutduration" | awk  '{printf "%i", ($1 - $2) * 1000}'` >> "$metafile"
    echo "title=Chapter $i" >> "$metafile"

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

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

    tempfiles+=("$chapterfile")
    concat="$concat|$chapterfile"

    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 "pre-${outfile}"
fi

# if we're cleaning up the video using handbrake
if [[ $usehandbrake ]]; then
  echo;echo "Encoding video with HandbrakeCLI..."
  $handbrakePath --preset "Apple 1080p30 Surround" --encoder-preset medium -i "pre-${outfile}" -o "${outfile}"
  rm -f "pre-${outfile}"
else
  mv "pre-${outfile}" "${outfile}"
fi

for i in "${tempfiles[@]}"; do
  rm -f "$i"
done

if [[ $deleteedl ]]; then
  rm -f "$edlfile";
fi

if [[ $deletemeta ]]; then
  rm -f "$metafile";
fi

if [[ $deletelog ]]; then
  rm -f "$logfile";
fi

if [[ $deletelogo ]]; then
  rm -f "$logofile";
fi

if [[ $deletetxt ]]; then
  rm "$txtfile";
fi

if [[ ! -z $ldPath ]]; then
  # re-set LD_LIBRARY_PATH
  export LD_LIBRARY_PATH="$ldPath"
fi

if [[ ! -z "$lockfile" ]]; then
  rm -f "$lockfile"
fi

from comchap.

BrettSheleski avatar BrettSheleski commented on July 20, 2024

Unfortunately I'm not going to add that to Comchap/Comcut. As stated previously Comchap/Comcut are to be a standalone tools to do one thing (flagging/removing commercials).

I understand both scripts can be used directly as a Plex post-processing script, but it is not limited to that. Adding this as suggested would introduce a new dependency to the project as well. Also, the quality settings for calling handbrake are hard-coded, making it difficult for someone to re-encode it to their liking.

If one wants to re-encode the video I'd suggest making a wrapper-script that does it instead. Something like the following:

recording = "$1"

comcut "$recording"
handbrakecli --preset "Apple 1080p30 Surround" --encoder-preset medium -i "$recording" -o "encoded-$recording"

rm "$recording"
mv "encoded-$recording" "$recording"

Then one can setup Plex to call this wrapper-script instead of calling Comchap/Comcut directly. This also makes it easier for the user to change their desired target quality.

from comchap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.