Advertisement
Guest User

webm.sh

a guest
Sep 16th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.82 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # /dbt/'s super awesome webm conversion script for bash v.1.0
  4. #
  5. # Just call $ bash ./webm.sh <source file>, supply basic info and boom, you get
  6. # the converted webm in the subfolder ./webm!
  7. # Alternatively, copy this to any directory in your PATH, make it executable
  8. # and just call it by its name.
  9. # See bottom of file for example terminal session.
  10. #
  11. # Limited choices for faster work.
  12. # Resolution can be chosen freely out of three options (1280x720, 960x540 and
  13. # 640x360) because it depends on source file frame rate.
  14. # File size limits were chosen with some safety margin in mind; you can edit
  15. # them if you want however (lines 52 and 56 -> maxsize).
  16. #
  17. # Choosing resolution:
  18. # Start at 960x540. Convert. Does your webm look like shit or stutter? Go DOWN
  19. # to 640x360, bitrate might be too low for this resolution. Is your webm way
  20. # below the size limit? Go up to 1280x720.
  21. # Rule of thumb for 30 fps video:
  22. # up to 10 seconds -> 720p
  23. # up to 30 seconds -> 540p
  24. # over 30 seconds -> 360p
  25. # If your webm is really long and exceeds the size limit, lower the resolution.
  26. # A certain minimum bitrate is required so you likely won't be able to keep a
  27. # 30 second 720p webm under 3 MB.
  28.  
  29. # get user input && calculate secondary params
  30. echo "output file name"
  31. read output
  32. echo "start time (hh:mm:ss.t)"
  33. read start
  34. echo "duration (seconds[.decimals])"
  35. read duration
  36. echo "y-resolution {720, 540, 360}"
  37. read v_res
  38. if [ "$v_res" = "720" ]; then
  39.     h_res="1280"
  40. elif [ "$v_res" = "540" ]; then
  41.     h_res="960"
  42. elif [ "$v_res" = "360" ]; then
  43.     h_res="640"
  44. else
  45.     echo "invalid resolution"
  46.     exit
  47. fi
  48. echo "audio? [y|N]"
  49. read audio
  50. if [ "$audio" = "y" ]; then
  51.     audiocmd="-c:a libvorbis -b:a 192K -vbr on"
  52.     maxsize=3800
  53. else
  54.     audio="N"
  55.     audiocmd="-an"
  56.     maxsize=3000
  57. fi
  58. bitrate=$(echo "(8*$maxsize/$duration)" | bc)
  59.  
  60. # write user params into logfile
  61. echo "converting $1:" >> ./webm_log.txt
  62. echo "-----------------------" >> ./webm_log.txt
  63. echo "start:       $start" >> ./webm_log.txt
  64. echo "duration:    $duration s" >> ./webm_log.txt
  65. echo "max bitrate: $bitrate kbps" >> ./webm_log.txt
  66. echo "output file: ./webm/$output.webm" >> ./webm_log.txt
  67. echo "resolution:  $h_res x $v_res" >> ./webm_log.txt
  68. echo "audio:       $audiocmd" >> ./webm_log.txt
  69. echo "" >> ./webm_log.txt
  70.  
  71. # write user params into stdout
  72. echo
  73. echo "converting $1:"
  74. echo "-----------------------"
  75. echo "start:       $start"
  76. echo "duration:    $duration s"
  77. echo "max bitrate: $bitrate kbps"
  78. echo "output file: ./webm/$output.webm"
  79. echo "resolution:  $h_res x $v_res"
  80. echo "audio:       $audiocmd"
  81. echo
  82. echo "Press [Enter] to start converting!"
  83. read confirm
  84.  
  85. # first pass
  86. echo "ffmpeg -hide_banner -loglevel \"info\" -y -ss \"$start\" -t \"$duration\" -i \"$1\" -c:v libvpx -pass 1 -qmin 0 -qmax 50 -crf 10 -b:v 1M -threads 3 -speed 4 -g 128 -an -sn -f webm \"/dev/null\""
  87. ffmpeg -hide_banner -loglevel "info" -y -ss "$start" -t "$duration" -i "$1" -c:v libvpx -pass 1 -qmin 0 -qmax 50 -crf 10 -b:v 1M -threads 3 -speed 4 -g 128 -an -sn -f webm "/dev/null"
  88.  
  89. # second pass
  90. mkdir "./webm"
  91. echo "ffmpeg -hide_banner -loglevel \"info\" -ss \"$start\" -t \"$duration\" -i \"$1\" -c:v libvpx -pass 2 -qmin 0 -qmax 50 -crf 10 -b:v \"$bitrate\" -s \"$h_res:$v_res\" $audiocmd -threads 3 -speed 0 -auto-alt-ref 1 -lag-in-frames 25 -g 128 -sn -f webm \"./webm/$output.webm\""
  92. ffmpeg -hide_banner -loglevel "info" -ss "$start" -t "$duration" -i "$1" -c:v libvpx -pass 2 -qmin 0 -qmax 50 -crf 10 -b:v "$bitrate"k -s "$h_res:$v_res" $audiocmd -threads 3 -speed 0 -auto-alt-ref 1 -lag-in-frames 25 -g 128 -sn -f webm "./webm/$output.webm"
  93.  
  94. # cleanup
  95. rm ffmpeg2pass-0.log
  96.  
  97. # Example terminal session, assuming this file and the video source are stored
  98. # in ~/Videos:
  99. #
  100. #kkb@xubuntu:~$ cd Videos && ll
  101. #total 1770580
  102. #drwxr-xr-x  3 kkb kkb       4096 Sep 16 19:21 ./
  103. #drwx------ 33 kkb kkb      12288 Sep 16 18:33 ../
  104. #-rw-rw-r--  1 kkb kkb 1148006607 Aug  5  2015 Steel Panther 2015 08 03 West Hollywood, CA, USA   House of Blues Webcast 720p-GNNedc13ENs.webm
  105. #-rw-rw-r--  1 kkb kkb  664883790 Jun 12 23:34 Steel Panther - Live from Lexxi’s Mom’s Garage DVD (Full Concert)-FXXcvntSlmY.mkv
  106. #drwxrwxr-x  3 kkb kkb      28672 Sep 16 19:21 webm/
  107. #-rw-rw-r--  1 kkb kkb      64366 Sep 10 22:26 webm.png
  108. #-rw-rw-r--  1 kkb kkb       3612 Sep 16 18:31 webm.sh
  109. #-rw-------  1 kkb kkb      11284 Sep 15 23:29 webm.txt
  110. #kkb@xubuntu:~/Videos$ bash ./webm.sh "Steel Panther - Live from Lexxi’s Mom’s Garage DVD (Full Concert)-FXXcvntSlmY.mkv"
  111. #output file name
  112. #steel_panther
  113. #start time (hh:mm:ss.t)
  114. #00:10:10
  115. #duration (seconds[.decimals])
  116. #10
  117. #y-resolution {720, 540, 360}
  118. #720
  119. #audio? [y|N]
  120. #y
  121. #
  122. #converting Steel Panther - Live from Lexxi’s Mom’s Garage DVD (Full Concert)-FXXcvntSlmY.mkv:
  123. #-----------------------
  124. #start:       00:10:10
  125. #duration:    10 s
  126. #max bitrate: 3040 kbps
  127. #output file: ./webm/steel_panther.webm
  128. #resolution:  1280 x 720
  129. #audio:       -c:a libvorbis -b:a 192K -vbr on
  130. #
  131. #Press [Enter] to start converting!
  132. #
  133. #ffmpeg -hide_banner -loglevel "info" -y -ss "00:10:10" -t "10" -i "Steel Panther - Live from Lexxi’s Mom’s Garage DVD (Full Concert)-FXXcvntSlmY.mkv" -c:v libvpx -pass 1 -qmin 0 -qmax 50 -crf 10 -b:v 1M -threads 3 -speed 4 -g 128 -an -sn -f webm "/dev/null"
  134. # <ffmpeg output>
  135. #mkdir: cannot create directory ‘./webm’: File exists
  136. #ffmpeg -hide_banner -loglevel "info" -ss "00:10:10" -t "10" -i "Steel Panther - Live from Lexxi’s Mom’s Garage DVD (Full Concert)-FXXcvntSlmY.mkv" -c:v libvpx -pass 2 -qmin 0 -qmax 50 -crf 10 -b:v "3040" -s "1280:720" -c:a libvorbis -b:a 192K -vbr on -threads 3 -speed 0 -auto-alt-ref 1 -lag-in-frames 25 -g 128 -sn -f webm "./webm/steel_panther.webm"
  137. # <ffmpeg output>
  138. #kkb@xubuntu:~/Videos$ mpv ./webm/steel_panther.webm
  139. # <mpv output>
  140. #kkb@xubuntu:~/Videos$ exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement