ffmpeg-video.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # # !/usr/bin/env zsh
  4. # zsh for fraction aritmetics ( i must learn bc for bash or awk)
  5. # # inputf="$*"
  6. # # inputnoext="${inputf%.*}"
  7. # # outputf="${inputnoext}.webm"
  8. # # For more visit link below
  9. # # https://unix.stackexchange.com/questions/28803/how-can-i-reduce-a-videos-size-with-ffmpeg
  10. display_help(){
  11. echo "Usage: ${0##*/} [-n|x|c|d=*|bv=*|ba=*] [file/s]"
  12. echo " --normal"
  13. echo " --x264"
  14. echo " --compress"
  15. echo " --downscale=(1.5,2,3...)]"
  16. echo " --bitrate-video=1M"
  17. echo " --bitrate-audio=320K"
  18. exit 1
  19. }
  20. [[ "$#" -lt 2 ]] && display_help
  21. for inputf in "${@:2}"; do
  22. outputf="${i%.*}_$1.${i//*.}"
  23. case "$1" in
  24. -n | --normal)
  25. ffmpeg -i "$inputf" "$outputf"
  26. ;;
  27. -x | --x264)
  28. ffmpeg -i "$inputf" \
  29. -c:v libx264 "$outputf"
  30. ;;
  31. -c | --compress)
  32. ffmpeg -i "$inputf" \
  33. -c:v libx264 \
  34. -crf 24 \
  35. -threads 4 -row-mt 1 \
  36. -tile-columns 6 -frame-parallel 1 \
  37. "$outputf"
  38. ;;
  39. -d=* | --downscale=*)
  40. # read -rp "downscale times? (1.5, 2, 3, 4....): " DSC
  41. # # x=1
  42. DSC="${1//*\=}"
  43. while true;do
  44. ffmpeg -i "$inputf" -vf scale="iw/$DSC:ih/$DSC" \
  45. "$outputf"
  46. # [[ "$?" -ne 0 ]] || break
  47. (( $? != 0 )) || break
  48. # DSC=$(bc<<<"$DSC+$x")
  49. # (( x+=1 ))
  50. (( DSC++ ))
  51. done
  52. ;;
  53. -bv=* | --bitrate-video=*)
  54. bitrate="${1//*\=}"
  55. ffmpeg -i "$inputf" -c copy -b:v "$bitrate" -y "$outputf"
  56. # ffmpeg -i "$x" -c copy -map 0:a -map 0:v -b "${1//*\-}" "$outputf"
  57. ;;
  58. -ba=* | --bitrate-audio=*)
  59. bitrate="${1//*\=}"
  60. ffmpeg -i "$inputf" -c copy -b:a "$bitrate" -y "$outputf"
  61. # ffmpeg -i "$x" -c copy -map 0:a -map 0:v -b "${1//*\-}" "$outputf"
  62. ;;
  63. *)
  64. display_help
  65. ;;
  66. esac
  67. done
  68. # >>3995397
  69. # >>3999547
  70. # >>4002188
  71. # >>4002201
  72. # Maybe we should make a new thread with a no nigger music edition. I'm rangebanned from making threads unfortunately. Also, for webms that aren't dependend on the music I encourage you to make a new version with less faggy / decadent / retarded music.
  73. # Not a particularily good example since the original (webm rel) wasn't that bad in the first place, but it literally took me 2 mins to splice the old webm together with a new audio track to make this one >>3999971.
  74. # I used ffmpeg with these settings:
  75. # >ffmpeg -i ORIG.webm -i NEWAUDIO.wav -map 0:v:0 -map 1:a -acodec libvorbis -c:v copy -fflags shortest -max_interleave_delta 500M -metadata title="ARTIST - TITLE" NEW.webm
  76. while getopts ':n:x:c:d:b:h' opt; do
  77. case "$opt" in
  78. i)inputf+=("$OPTARG")
  79. outputf+="${inputf%.*}_$1.${inputf//*.}" ;;
  80. n)ffmpeg -i "$inputf" "$outputf" ;;
  81. x)ffmpeg -i "$inputf" -c:v libx264 "$outputf" ;;
  82. c)ffmpeg -i "$inputf" \
  83. -c:v libx264 \
  84. -crf 24 \
  85. -threads 4 -row-mt 1 \
  86. -tile-columns 6 -frame-parallel 1 \
  87. "$outputf" ;;
  88. d) read -rp "downscale times? (1.5, 2, 3, 4....): " DSC
  89. while true;do
  90. ffmpeg -i "$inputf" -vf scale="iw/$DSC:ih/$DSC" "$outputf"
  91. (( $? != 0 )) || break
  92. (( DSC++ ))
  93. done ;;
  94. b) bitrate=(-b:v "${OPTARG}" -b:a "${OPTARG}") ;;
  95. h|\?|:|*)display_help ;;
  96. esac
  97. done
  98. shift $((OPTIND-1))
  99. # for file in "${inputf[@]}"; do
  100. # done