transcode2 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. # videoblerg.wordpress.com/2017/11/10/ffmpeg-and-how-to-use-it-wrong/comment-page-1/
  3. # script dependencies
  4. for cmd in mediainfo calc ffmpeg ffprobe; do
  5. which "$cmd" >/dev/null || exit 1
  6. done
  7. yesno() {
  8. answer=''
  9. prompt="$1 [yn]? "
  10. # superuser.com/questions/276531/clear-stdin-before-reading
  11. while read -r -t 0; do read -r; done
  12. until [[ "$answer" =~ ^[yn]$ ]]; do
  13. read -s -n1 -p "$prompt" answer
  14. [[ "$answer" != "" ]] && echo "$answer"
  15. done
  16. [[ "$answer" == n ]] && return 1
  17. return 0
  18. }
  19. file="$1"
  20. vcodec=x264
  21. pix_fmt=yuv420p
  22. ### CALCULATING VIDEO BITRATE ###
  23. crf=21 # so he said: videoblerg.wordpress.com/2014/12/18/extreme-encoding-settings-quality-and-size/
  24. ffmpeg -y -i "$file" -vcodec lib$vcodec -preset veryfast -pix_fmt "$pix_fmt" -profile:v baseline -an -crf $crf discard.mkv
  25. # gives us bits per pixel (bpp), width and height, framerate
  26. eval $(mediainfo --Output="Video;bpp=%Bits-(Pixel*Frame)%\nwidth=%Width%\nheight=%Height%\nframerate=%FrameRate%" discard.mkv)
  27. rm discard.mkv
  28. printf "bpp: $bpp\nwidth: $width\nheight: $height\nframerate: $framerate\n"
  29. vbitrate="$(calc -p -- "${bpp}*${width}*${height}*${framerate}/1024")"
  30. vbitrate="${vbitrate%.*}"
  31. #################################
  32. yesno "Calculated video bitrate: $vbitrate" || read -r -p "Enter video bitrate: " vbitrate
  33. #~ vbitrate=1500
  34. abitrate=128
  35. preset=veryslow
  36. # presets: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow
  37. tune=film
  38. # tunes: film,animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency
  39. bufsize=$((vbitrate+abitrate))
  40. maxrate=$((vbitrate + vbitrate/3))
  41. ffmpeg_cmd="ffmpeg -nostdin -hide_banner -analyzeduration 2147483647 -probesize 2147483647"
  42. endopts="-max_muxing_queue_size 9999"
  43. #~ -g 48 maximum keyframe interval? good for streaming, not needed in our use case.
  44. #~ -x264opts no-scenecut see: video.stackexchange.com/a/24684 - despite additional (minimal) overhead, scenecut seems to be important
  45. outfile="${file%.*}.$vcodec.$pix_fmt.v$vbitrate.a$abitrate.$preset.$tune.mp4"
  46. case $vcodec in
  47. x265)
  48. echo ffmpeg -i "$file" -pix_fmt "$pix_fmt" -vsync 1 -vcodec lib$vcodec -b:v: ${vbitrate}k -bufsize ${bufsize}k -maxrate ${maxrate}k -preset $preset -tune $tune -pass 1 -acodec aac -b:a ${abitrate}k -ac 2 -ar 48000 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" -f mp4 -y /dev/null
  49. $ffmpeg_cmd -i "$file" -pix_fmt "$pix_fmt" -vsync 1 -vcodec lib$vcodec -b:v: ${vbitrate}k -bufsize ${bufsize}k -maxrate ${maxrate}k -preset $preset -tune $tune -pass 1 -acodec aac -b:a ${abitrate}k -ac 2 -ar 48000 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" $endopts -f mp4 -y /dev/null
  50. echo ffmpeg -i "$file" -pix_fmt "$pix_fmt" -vsync 1 -vcodec lib$vcodec -b:v: ${vbitrate}k -bufsize ${bufsize}k -maxrate ${maxrate}k -preset $preset -tune $tune -pass 2 -acodec aac -b:a ${abitrate}k -ac 2 -ar 48000 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" -f mp4 "${file%.*}.testy.$vbitrate.$preset.mkv"
  51. $ffmpeg_cmd -i "$file" -pix_fmt "$pix_fmt" -vsync 1 -vcodec lib$vcodec -b:v: ${vbitrate}k -bufsize ${bufsize}k -maxrate ${maxrate}k -preset $preset -tune $tune -pass 2 -acodec aac -b:a ${abitrate}k -ac 2 -ar 48000 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" $endopts -f mp4 "$outfile"
  52. ;;
  53. x264)
  54. echo ffmpeg -i "$file" -pix_fmt "$pix_fmt" -vsync 1 -vcodec lib$vcodec -b:v: ${vbitrate}k -bufsize ${bufsize}k -maxrate ${maxrate}k -preset $preset -profile:v high -tune $tune -pass 1 -acodec aac -b:a ${abitrate}k -ac 2 -ar 48000 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" -f mp4 -y /dev/null
  55. $ffmpeg_cmd -i "$file" -pix_fmt "$pix_fmt" -vsync 1 -vcodec lib$vcodec -b:v: ${vbitrate}k -bufsize ${bufsize}k -maxrate ${maxrate}k -preset $preset -profile:v high -tune $tune -pass 1 -acodec aac -b:a ${abitrate}k -ac 2 -ar 48000 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" $endopts -f mp4 -y /dev/null
  56. echo ffmpeg -i "$file" -pix_fmt "$pix_fmt" -vsync 1 -vcodec lib$vcodec -b:v: ${vbitrate}k -bufsize ${bufsize}k -maxrate ${maxrate}k -preset $preset -profile:v high -tune $tune -pass 2 -acodec aac -b:a ${abitrate}k -ac 2 -ar 48000 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" -f mp4 "${file%.*}.testy.$vbitrate.$preset.mkv"
  57. $ffmpeg_cmd -i "$file" -pix_fmt "$pix_fmt" -vsync 1 -vcodec lib$vcodec -b:v: ${vbitrate}k -bufsize ${bufsize}k -maxrate ${maxrate}k -preset $preset -profile:v high -tune $tune -pass 2 -acodec aac -b:a ${abitrate}k -ac 2 -ar 48000 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" $endopts -f mp4 "$outfile"
  58. ;;
  59. esac