transcode2 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. # Consider this script a collection of ffmpeg settings for video transcoding.
  3. # It doesn't make much sense to share this.
  4. # It's grown from my very personal transcoding needs, it WILL require adjustment.
  5. # this was only the starting point:
  6. # videoblerg.wordpress.com/2017/11/10/ffmpeg-and-how-to-use-it-wrong/comment-page-1/
  7. # The useless part of the script currently depends on calc:
  8. # https://www.archlinux.org/packages/community/x86_64/calc/
  9. # script dependencies
  10. for cmd in mediainfo calc ffmpeg ffprobe; do
  11. which "$cmd" >/dev/null || exit 1
  12. done
  13. yesno() {
  14. answer=''
  15. prompt="$1 [yn]? "
  16. # superuser.com/questions/276531/clear-stdin-before-reading
  17. while read -r -t 0; do read -r; done
  18. until [[ "$answer" =~ ^[yn]$ ]]; do
  19. read -s -n1 -p "$prompt" answer
  20. [[ "$answer" != "" ]] && echo "$answer"
  21. done
  22. [[ "$answer" == n ]] && return 1
  23. return 0
  24. }
  25. file="$1"
  26. vcodec=x264
  27. pix_fmt=yuv420p
  28. ### CALCULATING VIDEO BITRATE ###
  29. crf=21 # so he said: videoblerg.wordpress.com/2014/12/18/extreme-encoding-settings-quality-and-size/
  30. ffmpeg -y -i "$file" -vcodec lib$vcodec -preset veryfast -pix_fmt "$pix_fmt" -profile:v baseline -an -crf $crf discard.mkv
  31. # gives us bits per pixel (bpp), width and height, framerate
  32. eval $(mediainfo --Output="Video;bpp=%Bits-(Pixel*Frame)%\nwidth=%Width%\nheight=%Height%\nframerate=%FrameRate%" discard.mkv)
  33. rm discard.mkv
  34. printf "bpp: $bpp\nwidth: $width\nheight: $height\nframerate: $framerate\n"
  35. vbitrate="$(calc -p -- "${bpp}*${width}*${height}*${framerate}/1024")"
  36. vbitrate="${vbitrate%.*}"
  37. #################################
  38. yesno "Calculated video bitrate: $vbitrate" || read -r -p "Enter video bitrate: " vbitrate
  39. #~ vbitrate=1500
  40. abitrate=128
  41. preset=veryslow
  42. # presets: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow
  43. tune=film
  44. # tunes: film,animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency
  45. bufsize=$((vbitrate+abitrate))
  46. maxrate=$((vbitrate + vbitrate/3))
  47. ffmpeg_cmd="ffmpeg -nostdin -hide_banner -analyzeduration 2147483647 -probesize 2147483647"
  48. endopts="-max_muxing_queue_size 9999"
  49. #~ -g 48 maximum keyframe interval? good for streaming, not needed in our use case.
  50. #~ -x264opts no-scenecut see: video.stackexchange.com/a/24684 - despite additional (minimal) overhead, scenecut seems to be important
  51. outfile="${file%.*}.$vcodec.$pix_fmt.v$vbitrate.a$abitrate.$preset.$tune.mp4"
  52. case $vcodec in
  53. x265)
  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 -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 -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 -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 -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. x264)
  60. 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
  61. $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
  62. 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"
  63. $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"
  64. ;;
  65. esac