Created
December 17, 2017 03:35
-
-
Save inaniwa3/62848e093aff074f46ca0f30b62df0a6 to your computer and use it in GitHub Desktop.
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
#! ruby -Ku | |
# coding: utf-8 | |
# usage: ruby kk.rb filename db d | |
# ex.) ruby kk.rb hoge.mp4 -30 1.0 | |
require "fileutils" | |
def argv | |
@filename = ARGV[0] | |
@db = ARGV[1] | |
@d = ARGV[2] | |
exit if @d.nil? | |
@extname = File.extname(@filename) | |
@basename = File.basename(@filename, @extname) | |
@workdir = "tmp" | |
end | |
def mkdir | |
FileUtils.rm_rf(@workdir) | |
FileUtils.mkdir_p(@workdir) | |
end | |
def detect | |
`ffmpeg.exe -i "#{@filename}" -af silencedetect=n=#{@db}dB:d=#{@d} -f null - 2>"#{@workdir}/#{@basename}.txt"` | |
end | |
def parse | |
lines = open("#{@workdir}/#{@basename}.txt").read.split(/[\r\n]/) | |
@ss_t = [] | |
lines.each do |line| | |
if m = line.match(/\[silencedetect @ .+\] silence_start: (.+)/) | |
@ss_t << m[1] | |
elsif m = line.match(/\[silencedetect @ .+\] silence_end: .+ \| silence_duration: (.+)/) | |
@ss_t << m[1] | |
end | |
end | |
@ss_t << nil if @ss_t.length % 2 == 1 | |
end | |
def cut | |
@cutfilenames = [] | |
(@ss_t.length / 2).times do |i| | |
ss = @ss_t[i * 2] | |
t = @ss_t[i * 2 + 1] | |
@cutfilenames << "#{@basename}.#{i}#{@extname}" | |
`ffmpeg.exe -ss #{ss} -i "#{@filename}" #{t.nil? ? "" : "-t #{t}"} "#{@workdir}/#{@cutfilenames.last}" > nul 2>&1` | |
end | |
end | |
def listname | |
s = @cutfilenames.map{|f| "file '#{f}'\n"}.join() | |
@listname = "#{@basename}.list" | |
File.write("#{@workdir}/#{@listname}", s) | |
end | |
def concat | |
`ffmpeg -f concat -i "#{@workdir}/#{@listname}" -c copy "#{@basename}.#{@db}.#{@d}#{@extname}" > nul 2>&1` | |
end | |
def execute | |
argv | |
mkdir | |
detect | |
parse | |
cut | |
listname | |
concat | |
end | |
execute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment