Last active
August 11, 2022 01:04
-
-
Save meinside/0a92ba2eeb65e914c2fa to your computer and use it in GitHub Desktop.
Slice & convert given video files to .webm format. WebM 짤방 제조용 스크립트.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# coding: UTF-8 | |
# 2webm.rb | |
# | |
# convert given video file to .webm format | |
# | |
# (ffmpeg should be installed with libvpx and libvorbis. | |
# | |
# ex: $ brew install ffmpeg --with-libvpx --with-libvorbis) | |
# | |
# created on : 2014.07.15. | |
# last update: 2022.08.11. | |
# | |
# by [email protected] | |
require 'bundler/setup' | |
require 'thor' | |
module Ffmpeg | |
VERBOSE = true | |
class ToWebM < Thor | |
default_task :convert | |
desc "convert", "convert a video file to .webm format" | |
long_desc <<CONVERT_DESC | |
* Usage | |
# will convert original.mp4 to original.mp4.webm | |
$ #{__FILE__} -i original.mp4 | |
# will convert original.mp4 to converted.webm | |
$ #{__FILE__} -i original.mp4 -o converted.webm | |
# will convert original.mp4 into 320x240 | |
$ #{__FILE__} -i original.mp4 -w 320 -h 240 | |
# will slice & convert original.mp4 from 01:20:40.0 to 01:20:50.0 (for 10.0 seconds) | |
$ #{__FILE__} -i original.mp4 -s 01:20:40.0 -d 10.0 | |
CONVERT_DESC | |
method_option :in_filepath, type: :string, aliases: '-i', desc: 'input video file\'s path', required: true | |
method_option :out_filepath, type: :string, aliases: '-o', desc: 'output video file\'s path' | |
method_option :start, type: :string, aliases: '-s', desc: 'start point of input video' | |
method_option :duration, type: :string, aliases: '-d', desc: 'length of input video' | |
method_option :width, type: :numeric, aliases: '-w', desc: 'width of output video' | |
method_option :height, type: :numeric, aliases: '-h', desc: 'height of output video' | |
def convert | |
# time | |
start = options[:start] ? parse_time(options[:start]) : nil | |
duration = options[:duration] ? parse_time(options[:duration]) : nil | |
# size | |
width = options[:width] | |
height = options[:height] | |
size = (width && height) ? "#{width}x#{height}" : nil | |
# filepath | |
in_filepath = File.expand_path(options[:in_filepath]) | |
out_filepath = options[:out_filepath] ? File.expand_path(options[:out_filepath]) : "#{in_filepath}.webm" | |
# run ffmpeg | |
cmd = "ffmpeg #{start ? "-ss #{start}" : ''} #{duration ? "-t #{duration}" : ''} -i \"#{in_filepath}\" -c:v libvpx -minrate 1M -maxrate 1M -b:v 1M -c:a libvorbis #{size ? "-s #{size}" : ''} \"#{out_filepath}\"" | |
puts "* running command: #{cmd}" if VERBOSE | |
`#{cmd}` | |
end | |
private | |
def parse_time(time) | |
separated = (time || '').split(':') | |
seconds = (separated[-1] || 0).to_f | |
minutes = (separated[-2] || 0).to_i | |
hours = (separated[-3] || 0).to_i | |
minutes += seconds / 60 | |
hours += minutes / 60 | |
seconds = seconds % 60.0 | |
minutes = minutes % 60 | |
"%02d:%02d:%07.4f" %[hours, minutes, seconds] | |
end | |
end | |
end | |
trap('SIGINT') { puts; exit 1 } | |
Ffmpeg::ToWebM.start(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# last update: 2014.07.15. | |
source 'http://rubygems.org' | |
gem 'thor' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment