Advertisement
Guest User

Untitled

a guest
Aug 17th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.24 KB | None | 0 0
  1. #/usr/bin/env sh
  2. #check if argument for input file is set; fail if not
  3. if [ ! -z ${1+x} ]
  4. then
  5.     INPUT_FILE="$1"
  6.  
  7.     #if second argument is given, use as output file name; use default if not
  8.     if [ ! -z ${2+x} ]
  9.     then
  10.         OUTPUT_FILE="$2"
  11.     else
  12.         OUTPUT_FILE="output.webm"
  13.     fi
  14.    
  15.     #make temporary directories
  16.     TMP_DIR=$(mktemp -d)
  17.     mkdir ${TMP_DIR}/{frames,scaled}
  18.  
  19.     #extract individual frames from inputfile
  20.     ffmpeg -v error -i "${INPUT_FILE}" -qmin 1 -q:v 1 "${TMP_DIR}/frames/%04d.jpeg"
  21.  
  22.     #count frames
  23.     FRAMES=$(ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -count_frames -show_entries stream=nb_read_frames -i "${INPUT_FILE}")
  24.     printf 'Frame count: %s\n' ${FRAMES}
  25.    
  26.     #get framerate from input file; used for intermediary files, can't be changed during concat
  27.     FPS=$(ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate -i "${INPUT_FILE}")
  28.     printf 'Frame rate: %s\n' ${FPS}
  29.  
  30.     #scale each frame according to its index and transcode to webm again
  31.     #this is EXTREMELY inefficient; the overhead of restarting ffmpeg possibly thousands of times means that even on a powerful machine this script will take ages to run
  32.     for f in $(find ${TMP_DIR}/frames -name '*.jpeg' | sed -En 's#^.*/?frames/([0-9]+).jpeg$#\1#p' | sort); do ffmpeg -v error -i "${TMP_DIR}/frames/${f}.jpeg" -frames:v 1 -filter:v "scale=$(printf '%s + 10\n' ${f} | bc):-1" -c:v libvpx -crf 31 -r "${FPS}" "${TMP_DIR}/scaled/${f}.webm"; printf 'Progress: %s/%s [%s%%]     \r' ${f} ${FRAMES} $(printf 'scale=2;%s*100/%s\n' ${f} ${FRAMES} | bc); done; printf '\n'
  33.    
  34.     #concatenate all intermediary files; try to map audio from input file; if input file has no audio rerun without mapping it
  35.     ffmpeg -thread_queue_size ${FRAMES} -f concat -safe 0 -i <(find ${TMP_DIR}/scaled/ -name '*.webm' -printf "file '%p'\n" | sort) -i "${INPUT_FILE}" -c:v copy -c:a libopus -map "0:v" -map "1:a" ${OUTPUT_FILE} || ffmpeg -thread_queue_size ${FRAMES} -f concat -safe 0 -i <(find ${TMP_DIR}/scaled/ -name '*.webm' -printf "file '%p'\n" | sort) -c:v copy  ${OUTPUT_FILE}
  36.  
  37.     #remove intermediary files
  38.     rm -r "${TMP_DIR}"
  39.    
  40.     #exit normaly
  41.     exit 0
  42. else
  43.     #exit with error
  44.     printf 'Usage: %s [INPUT FILE] {OUTPUT FILE}\n' $0
  45.     exit 1
  46. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement