Advertisement
Guest User

FFmpeg shell script

a guest
Jul 17th, 2012
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. > !/bin/bash
  2. #
  3. #
  4. # ffmpeg libxvid
  5. # --------------------------------------------------------------------------------------------------------
  6. # ffmpeg -i Input-Filename.avi -f mp4 -y \
  7. # -vcodec libxvid -b:v 600k -acodec libfaac -b:a 96k -ac 2 -ar 44100 \
  8. # -r 25 -s 640x272 -aspect 640:360 -vf pad=640:360:0:44 \
  9. # -threads 2 -async 1 -pass 1 /dev/null
  10. # ffmpeg -i Input-Filename.avi -f mp4 \
  11. # -y -vcodec libxvid -b:v 600k -acodec libfaac -b:a 96k -ac 2 -ar 44100 \
  12. # -r 25 -s 640x272 -aspect 640:360 -vf pad=640:360:0:44 \
  13. # -threads 2 -async 1 -pass 2 ./Input-Filename-ffmpeg.mp4
  14. #
  15. # ffmpeg libx264
  16. # ----------------------------------------------------------------------------------------------------------
  17. # ffmpeg -i Input-Filename.avi -f mp4 -y \
  18. # -vcodec libx264 -b:v 600k -acodec libfaac -b:a 96k -ac 2 -ar 44100 \
  19. # -r 25 -s 320x196 -aspect 320:240 -vf pad=320:240:0:22 \
  20. # -threads 2 -async 1 -pass 1 /dev/null
  21. # ffmpeg -i Input-Filename.avi -f mp4 -y \
  22. # -vcodec libx264 -b:v 600k -acodec libfaac -b:a 96k -ac 2 -ar 44100 \
  23. # -r 25 -s 320x196 -aspect 320:240 -vf pad=320:240:0:22 \
  24. # -threads 2 -async 1 -pass 2 ./Input-Filename-ffmpeg.mp4
  25. Usage
  26. # --------------------------------------------------------------------------------------------------------
  27. # Command-line options:
  28. # -a : Video aspect ratio. Could be either 1.77 or 2.35 (default)
  29. # -b : Video bitrate. Should be in the form of 600k (default)
  30. # -c : Video codec. Should be either libx264 or libxvid (default)
  31. # -d : Output directory. Current directory (.) is the default
  32. # -y : Whether to ask confirmation before overwriting any file.
  33. #
  34. Should be either "yes" (default) or "no"
  35. # Command-line options
  36. while getopts 'a:b:c:d:o:p:y' opt "$@"; do
  37. case "$opt" in
  38. a) video_aspect="$OPTARG" ;;
  39. b) vbitrate="$OPTARG" ;;
  40. c) video_codec="$OPTARG" ;;
  41. d) output_dir="$OPTARG" ;;
  42. o) addl_options="$OPTARG" ;;
  43. p) passes="$OPTARG" ;;
  44. y) ask_confirmation="no" ;;
  45. esac
  46. done
  47. shift $((OPTIND - 1))
  48. # Defaults
  49. video_aspect="${video_aspect:-2.35}"
  50. video_codec="${video_codec:-libxvid}" # or libx264
  51. vbitrate="${vbitrate:-600k}"
  52. passes="${passes:-2}"
  53. output_dir="${output_dir:-.}"
  54. vpre=""
  55. if [[ "$video_codec" == "libx264" ]]; then
  56. vpre="-vpre ipod320"
  57. aspect="320:240"
  58. if [[ "$video_aspect" == "2.35" ]]; then
  59. resolution="320x196"
  60. pad="pad=320:240:0:22"
  61. elif [[ "$video_aspect" == "1.77" ]]; then
  62. resolution="320x240"
  63. pad="pad=320:240:0:0"
  64. fi;
  65. elif [[ "$video_codec" == "libxvid" ]]; then
  66. aspect="640:360"
  67. if [[ "$video_aspect" == "2.35" ]]; then
  68. resolution="640x272"
  69. pad="pad=640:360:0:44"
  70. elif [[ "$video_aspect" == "1.77" ]]; then
  71. resolution="640x360"
  72. pad="pad=640:360:0:0"
  73. fi;
  74. fi;
  75. echo "Encoding '${#@}' video(s)";
  76. for in_file in "$@"; do
  77. # If the filename has no extension
  78. if [[ -z "$(echo "$in_file" | grep -Ei "\.[a-z]+$")" ]]; then
  79. fname="$(basename "${in_file}")-ffmpeg.mp4"
  80. else
  81. fname="$(basename "$in_file" | sed -sr 's/^(.*)(\.[^.]+)$/\1-ffmpeg.mp4/')"
  82. fi
  83. out_file="${output_dir%/}/${fname}"
  84. # Avoid overwriting files
  85. if [[ "$ask_confirmation" != "no" ]] && [[ -f "$out_file" ]]; then
  86. echo -n "'$out_file' already exists. Do you want to overwrite it? [y/n] "; read response
  87. [[ -z "$(echo "$response" | grep -i "^y")" ]] && continue
  88. fi
  89. # 1st pass
  90. ffmpeg -i "$in_file" \
  91. -f mp4 -y $addl_options \
  92. -vcodec "$video_codec" -b:v "$vbitrate" $vpre \
  93. -acodec libfaac -b:a 96k -ac 2 -ar 44100 \
  94. -r 25 -s "$resolution" -aspect "$aspect" -vf "$pad" \
  95. -threads 2 -async 1 -pass 1 \
  96. "/dev/null"; # $out_file;
  97. # 2nd pass
  98. ffmpeg -i "$in_file" \
  99. -f mp4 -y $addl_options \
  100. -vcodec "$video_codec" -b:v "$vbitrate" $vpre \
  101. -acodec libfaac -b:a 96k -ac 2 -ar 44100 \
  102. -r 25 -s "$resolution" -aspect "$aspect" -vf "$pad" \
  103. -threads 2 -async 1 -pass 2 \
  104. "$out_file";
  105. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement