4chan-webm.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # defaults for easy creating webm to 4chan
  4. INPUT_OPTS=(-y)
  5. OUTPUT_OPT=(-c:v libvpx-vp9 -c:a libvorbis -sn -b:a 48k -b:v 320k -fs 6000000)
  6. EXT="webm"
  7. CUSTOM_FN=""
  8. display_help(){
  9. echo "Usage: ${0##*/} <file/s>"
  10. echo "Default behaviour: makes webm from audio or video"
  11. echo "Where:"
  12. echo " -a --Specifies audio bitrate"
  13. echo " -v --Specifies video bitrate"
  14. echo " -s --Specifies webm max size"
  15. echo " -d --Scale down webm"
  16. echo " -b --Start from time (can be in seconds or HH:MM:SS)"
  17. echo " -e --End to time (can be in seconds or HH:MM:SS)"
  18. echo " -t --Duration time"
  19. echo " -c --Compression level (set number between 4 and 63, smaller number==better quality but larger filesize)"
  20. echo " -o --Custom output file"
  21. echo " -C --Copy audio and video codecs"
  22. echo " -N --No audio (SPECIFY AT THE BEGGINIG)"
  23. echo " -O --Output filename with current date and time"
  24. echo " -R --Reverse video and audio (not recommended on large/long files)"
  25. echo " -V --Reverse video"
  26. echo " -A --Reverse audio"
  27. echo " -F --Speedup webm 2 times"
  28. echo " -S --Slowdown webm 2 times"
  29. echo " -1 --Change speed of video (float values from 0 to 2)"
  30. echo " -2 --Change speed of audio (float values from 0.5 to 2)"
  31. echo ""
  32. echo "Default or specified OUTPUT_OPT:"
  33. echo "\$ ffmpeg ${INPUT_OPTS[*]} -i $input_file ${OUTPUT_OPT[*]} ${output_file}"
  34. exit 2
  35. }
  36. while getopts 'a:v:s:d:c:b:t:o:e:1:2:NOFRSVAh' opt; do
  37. case "$opt" in
  38. a) OUTPUT_OPT+=(-b:a "${OPTARG}") ;;
  39. v) OUTPUT_OPT+=(-b:v "${OPTARG}") ;;
  40. s) OUTPUT_OPT+=(-fs "${OPTARG}") ;; # TODO if filesize hits limit but the time not, create new webm from the stop time?
  41. d) OUTPUT_OPT+=(-vf "scale=iw/${OPTARG}:ih/${OPTARG}") ;;
  42. c) OUTPUT_OPT+=(-crf "${OPTARG}") ;;
  43. b) INPUT_OPTS+=(-ss "${OPTARG}") ;; # TODO -e end flag ffmpeg -to
  44. e) INPUT_OPTS+=(-to "${OPTARG}") ;; # TODO -e end flag ffmpeg -to
  45. t) OUTPUT_OPT+=(-t "${OPTARG}") ;;
  46. o) CUSTOM_FN="$OPTARG" ;; # TODO
  47. 1) OUTPUT_OPT+=(-filter:v "setpts=${OPTARG}*PTS") ;;
  48. 2) OUTPUT_OPT+=(-filter:a "atempo=${OPTARG}") ;;
  49. N) OUTPUT_OPT+=(-c:v libvpx -map 0:0) ;; # if something is wrong maybe delete + from this line
  50. O) CUSTOM_FN="CURRENT_DATE" ;; # TODO
  51. F) OUTPUT_OPT+=(-filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]") ;;
  52. S) OUTPUT_OPT+=(-filter_complex "[0:v]setpts=2.0*PTS[v];[0:a]atempo=0.5[a]" -map "[v]" -map "[a]") ;;
  53. R) OUTPUT_OPT+=(-vf reverse -af areverse) ;;
  54. V) OUTPUT_OPT+=(-vf reverse) ;;
  55. A) OUTPUT_OPT+=(-af areverse) ;;
  56. h|\?|:|*) display_help ;;
  57. esac
  58. done
  59. shift $((OPTIND-1))
  60. create_dirs(){
  61. file="$1"
  62. dir=$(dirname "$file")
  63. mkdir -p "$dir"
  64. }
  65. embed_subtitles(){
  66. input_file="$1"
  67. subtitle_file="${input_file%.*}.srt"
  68. [[ -f $subtitle_file ]] && OUTPUT_OPT+=(-vf "subtitles=${subtitle_file}")
  69. }
  70. run_ffmpeg(){
  71. input_file="$1"
  72. ffmpeg "${INPUT_OPTS[@]}" \
  73. -i "$input_file" \
  74. "${OUTPUT_OPT[@]}" \
  75. -threads 4 \
  76. -max_muxing_queue_size 9999 \
  77. "$output_file"
  78. }
  79. [[ "$#" -lt 1 ]] && display_help
  80. # variable for make an index during specyfying custom filename (one) for multiple files
  81. nr=1
  82. for input_file in "$@"; do
  83. filetype=$(file --mime-type -b "$input_file" | cut -d/ -f1)
  84. case "$filetype" in
  85. video) dir="${HOME}/Documents/4chan/video" ;;
  86. audio) dir="${HOME}/Documents/4chan/audio" && OUTPUT_OPT+=(-r 1) ;;
  87. *) echo "Wrong filetype: ${filetype}"
  88. echo "File should be of 'audio' or 'video' filetype"
  89. continue ;;
  90. esac
  91. mkdir -p "$dir"
  92. if [[ $CUSTOM_FN == "CURRENT_DATE" ]]; then # specified -O flag
  93. output_file="${dir}/$(date +'%Y-%m-%d-%H%M%S')_${input_file%.*}.${EXT}"
  94. elif [[ -n $CUSTOM_FN ]]; then #specified -o flag
  95. output_file="${dir}/${CUSTOM_FN%.*}-${nr}.${EXT}"
  96. (( nr+=1 ))
  97. else
  98. output_file="${dir}/${input_file%.*}.${EXT}"
  99. fi
  100. create_dirs "$output_file"
  101. # embed_subtitles "$input_file"
  102. run_ffmpeg "$input_file"
  103. done
  104. # path of the clip goes to the clipboard
  105. echo "$output_file" | xclip -selection clipboard
  106. # # some guy from 4chan said...
  107. # ffmpeg -i INPUT -c:v libvpx -crf 4 -b:v 1500K -vf scale=320:-1 -an output.webm