SHARE
TWEET

webm-lua

a guest May 25th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2.  
  3. #Usage: webm [INPUT] [start time in ss.fff] [duration in s.fff] [filename] [OPTIONAL additional settings placed in quotes e.g "-vf scale:-1:360 -threads 8"]
  4. #requires: bc, gdate, youtube-dl, ffmpeg
  5.  
  6. LENGTH=$(bc <<< "scale=3; $3-$2")
  7. BITRATE=$(bc <<< "scale=3; 24/$LENGTH")
  8. MAXSIZE=3145728
  9. MINSIZE=3093299
  10. IDEAL=3133440
  11. sizeNOW=0
  12. sizeOLD=0
  13. ATTEMPTS=0
  14. oldBR=0
  15. TIMESTAMP=$2
  16.  
  17. if (( $(bc <<< "$TIMESTAMP < 4") )) #to prevent negative timestamps
  18. then
  19.     UNIT=0
  20.     BACKSTEP=0
  21.     NEWTIME=$2
  22. else
  23.     UNIT=4
  24.     BACKSTEP=4 #backstep in seconds since epoch
  25.     NEWTIME=$(bc <<< "$TIMESTAMP - $BACKSTEP") #timestamp shifted 4 or 0 seconds back
  26. fi
  27.  
  28. while ([ $sizeNOW -lt $MINSIZE ] || [ $sizeNOW -gt $MAXSIZE ]) && [ $ATTEMPTS -lt 5 ]; do
  29.  
  30.     if [[ $1 == *"http"* ]]
  31.     then
  32.         INPUT="$(youtube-dl -f bestvideo/best -g "$1")"
  33.     else
  34.         INPUT="${1//\\\ /\ }"
  35.     fi
  36.  
  37.     sizeOLD=$sizeNOW
  38.     safetyBR=$oldBR
  39.     oldBR=$BITRATE
  40.  
  41.     echo "ffmpeg -ss "$NEWTIME" -i "$INPUT" -ss "$UNIT" -c:v libvpx -an -sn -t "$LENGTH" -b:v "$BITRATE"M -y -v fatal -stats ~/Desktop/"$4".webm"
  42.  
  43.     ffmpeg -ss "$NEWTIME" -i "$INPUT" -ss "$UNIT" -c:v libvpx -an -sn -t "$LENGTH" -b:v "$BITRATE"M -y -v fatal -stats ~/Desktop/"$4".webm
  44.  
  45.     sizeNOW=$(wc -c <~/Desktop/"$4".webm)
  46.     BITRATE=$(bc <<< "scale=3;($IDEAL/$sizeNOW)*$BITRATE")
  47.  
  48.     if (( $(bc <<< "$oldBR > $safetyBR") ))
  49.     then
  50.         INCREASING=1
  51.     else
  52.         INCREASING=0
  53.     fi
  54.  
  55.     ((ATTEMPTS++))
  56.  
  57.     if (( $sizeNOW < $sizeOLD )) && (( $INCREASING == 1 )) #to avoid errors caused by bitrate being set too high
  58.     then
  59.         echo
  60.         echo "#"$ATTEMPTS"    $(($sizeNOW / 1024))kB    @"$oldBR"Mb/s"
  61.         echo "Maximum bitrate exceeded. Next @"$safetyBR"Mb/s"
  62.         ffmpeg -ss "$NEWTIME" -i "$INPUT" -ss "$UNIT" -c:v libvpx -an -sn -t "$LENGTH" -b:v "$safetyBR"M -y -v fatal -stats ~/Desktop/"$4".webm
  63.         sizeNOW=$(wc -c <~/Desktop/"$4".webm)
  64.         ((ATTEMPTS++))
  65.         echo
  66.         echo "#"$ATTEMPTS"    $(($sizeNOW / 1024))kB    @"$safetyBR"Mb/s"
  67.         echo "$4.webm    $(($sizeNOW / 1024))kB"
  68.         exit
  69.     fi
  70.  
  71.     echo
  72.     echo "#"$ATTEMPTS"    $(($sizeNOW / 1024))kB    @"$oldBR"Mb/s"
  73.     echo "Next @"$BITRATE"Mb/s"
  74. done
  75.  
  76. echo
  77. echo "$4.webm    $(($sizeNOW / 1024))kB"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top