Videos schneiden mit Mplayer

vidcut

#!/bin/bash
 
#part of the Mutils by Paul Hänsch
#to be ditributed under the terms of the GPL v.2 and any later version
 
frontend=Xdialog
 
#convert input filename
ifile=`echo "$1" |sed '/media:\/hdc/s//\/mnt\/cdrom/g;/-/s//\\-/g;/ /s//\\ /g'`
 
#building tempfiles
mpipe=`mktemp -t mpipeXXXX`
rm -f $mpipe
mkfifo $mpipe
fpipe=`mktemp -t fpipeXXXX` #cannot be a pipe because `cat` would never terminate when reading from an open pipe
dialog=`mktemp -t diagXXXX`
 
 
echo "pausing frame_step" >$mpipe &
mplayer "$ifile" -slave -input file=$mpipe -identify >>$fpipe &
 
sleep 1
fps=`cat $fpipe |grep "ID_VIDEO_FPS=" |cut -d "=" -f2 |cut -d "." -f1`
 
frame=0
 
###############################################################################################################
 
choice=1
result=0
while [ $result -eq 0 ]; do
# if [ $choice -eq 1 ]; then
 case $choice in
  1) menu="--radiolist Startposition_wählen 0 60 0 1 Keyframe_vorwärts on 2 Keyframe_zurück off";;
  *) menu="--radiolist Startposition_wählen 0 60 0 1 Keyframe_vorwärts off 2 Keyframe_zurück on";;
 esac
 
 if [ $frontend == dialog ]; then
  $frontend --extra-button --extra-label "Anfangsmarke" `echo $menu` 2>$dialog
  result=$?
 else
  $frontend --check "dieses Frame als Anfangsmarke" off `echo $menu` 2>$dialog
  result=$?
 fi
 grep "unchecked" $dialog || result=2
 
 if [ $result -eq 0 ]; then
  choice=`dd if=$dialog ibs=1 count=1`
  if [  $choice -eq 1 ]; then
   echo "pausing seek +1" >>$mpipe
   echo "frame_step" >>$mpipe
  else
   echo "pausing seek -1" >>$mpipe
   echo "frame_step" >>$mpipe
  fi
 fi
done
 
if [ $result -eq 1 ]; then #user requested quit
 echo "quit" >$mpipe
 rm -f $mpipe
 rm -f $fpipe
 rm -f $dialog
 exit 0
fi
 
#grep current frame
sleep 1 #let mplayer write all status lines
frame=`cat $fpipe |grep "A:" |sed '/\/  /s//\//g;/\/ /s//\//g' |cut -d "/" -f2 |cut -d " " -f1`
frame=`echo $frame |sed '/ /s//#/g'`
cnt=1
while [ ! -z `echo $frame |cut -s -d "#" -f$cnt` ]; do
 cnt=`expr $cnt + 1`
done
cnt=`expr $cnt - 1`
frame=`echo $frame |cut -s -d "#" -f$cnt`
 
frmcnt=`expr $frame - 1`
sec=`expr $frmcnt / $fps`
 
#################################################################################################################
 
choice=1
result=0
while [ $result -eq 0 ]; do
 
 case $choice in
  1) menu="--radiolist Endposition_wählen 0 0 0 1 Keyframe_vorwärts on 2 Keyframe_zurück off 3 Einzelframe_vorwärts off";;
  2) menu="--radiolist Endposition_wählen 0 0 0 1 Keyframe_vorwärts off 2 Keyframe_zurück on 3 Einzelframe_vorwärts off";;
  *) menu="--radiolist Endposition_wählen 0 0 0 1 Keyframe_vorwärts off 2 Keyframe_zurück off 3 Einzelframe_vorwärts on";;
 esac
 
 if [ $frontend == dialog ]; then
  $frontend --extra-button --extra-label "Endmarke" $menu 2>$dialog
  result=$?
 else
  $frontend --check "dieses Frame als Endmarke" off $menu 2>$dialog
  result=$?
 fi
 grep "unchecked" $dialog || result=2
 
 if [ $result -eq 0 ]; then
  choice=`dd if=$dialog ibs=1 count=1`
  if [  $choice -eq 1 ]; then
   echo "pausing seek +1" >>$mpipe
   echo "frame_step" >>$mpipe
  fi
  if [  $choice -eq 2 ]; then
   echo "pausing seek -1" >>$mpipe
   echo "frame_step" >>$mpipe
  fi
  if [  $choice -eq 3 ]; then
   echo "frame_step" >>$mpipe
  fi
 fi
done
 
if [ $result -eq 1 ]; then #user requested quit
 echo "quit" >$mpipe
 rm -f $mpipe
 rm -f $fpipe
 rm -f $dialog
 exit 0
fi
 
#grep current frame
sleep 1 #let mplayer write all status lines
frame=`cat $fpipe |grep "A:" |sed '/\/  /s//\//g;/\/ /s//\//g' |cut -d "/" -f2 |cut -d " " -f1`
frame=`echo $frame |sed '/ /s//#/g'`
cnt=1
while [ ! -z `echo $frame |cut -s -d "#" -f$cnt` ]; do
 cnt=`expr $cnt + 1`
done
cnt=`expr $cnt - 1`
frame=`echo $frame |cut -s -d "#" -f$cnt`
 
frmcnt=`expr $frame - 1 - $frmcnt`
 
#################################################################################################################
 
echo "quit" >$mpipe
rm -f $mpipe
rm -f $fpipe
 
ofile=~/
while [ -d $ofile ]; do
 $frontend --fselect "$ofile" 15 70 2>$dialog
 if [ $? == 1 ];then
  rm -f $dialog
  exit 0
 fi
 ofile=`cat $dialog`
done
 
rm -f $dialog
 
if [ $frontend == dialog ]; then
 $frontend --infobox "-Bitte Warten-" 0 0
else
 $frontend --no-buttons --infobox "-Bitte Warten-" 0 0 5000 &
fi
mencoder "$ifile" -ss $sec -frames $frmcnt -o "$ofile" 2>>/dev/null >>/dev/null
 
if [ $? -eq 0 ]; then
 $frontend --ok-label " Dankeschön " --msgbox "    Videoschnitt erfolgreich abgeschlossen.  " 6 53
else
 $frontend --ok-label " Tja! " --msgbox "Irgendwas ging schief.\n - sorry -" 0 0
fi