Last active
December 2, 2016 08:19
-
-
Save paradox460/9218bd2e98f5ffc256fb9cb0aab63f48 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
#! /usr/bin/env ruby | |
require 'logger' | |
require 'shellwords' | |
files = Dir.glob('*.srt') - ARGV | |
logger = Logger.new(STDOUT) | |
def shell_quote(string) | |
return '' if string.nil? | |
string = string.to_s if string.respond_to? :to_s | |
if string.empty? | |
"''" | |
else | |
string.split("'", -1).map { |m| "'#{m}'" }.join("\\'") | |
end | |
end | |
ARGV.each do |f| | |
begin | |
basename = File.basename(f, '.*') | |
ext = File.extname(f) | |
newname = f.gsub(/#{ext}$/i, ".subbed#{ext}") | |
subs = files.select do |x| | |
x =~ /#{basename}[_\.].{3}\.srt/i | |
end | |
subs = subs.uniq.sort | |
# Promote english | |
eng_index = subs.find_index { |x| x.match(/eng/) } | |
subs.unshift(subs.delete_at(eng_index)) unless eng_index.nil? | |
inputs = [] | |
languages = [] | |
subs.each do |s| | |
lang = s.match(/.*[_\.](.{3})\.srt\z/)[1] | |
inputs << shell_quote(s) | |
languages << shell_quote(lang) | |
end | |
args = ['-i', shell_quote(f)] | |
# Add inputs | |
args += inputs.flat_map do |i| | |
['-i', i] | |
end | |
# Map Statements | |
args += Array.new(inputs.count + 1) do |i| | |
['-map', i.to_s] | |
end.flatten | |
# Inject copy mode | |
args += ['-c', 'copy'] | |
# Metadata | |
args += languages.flat_map.with_index do |l, i| | |
["-metadata:s:s:#{i}", "language=#{l}"] | |
end | |
# Add final name | |
args << shell_quote(newname) | |
options = args.join(' ') | |
logger.debug "ffmpeg #{options}" | |
IO.popen("ffmpeg #{options}", 'r') do |pipe| | |
logger.debug pipe.read | |
end | |
end | |
end |
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 | |
require 'logger' | |
require 'shellwords' | |
logger = Logger.new(STDOUT) | |
def shell_quote(string) | |
return '' if string.nil? | |
string = string.to_s if string.respond_to? :to_s | |
if string.empty? | |
"''" | |
else | |
string.split("'", -1).map { |m| "'#{m}'" }.join("\\'") | |
end | |
end | |
ARGV.each do |f| | |
begin | |
ext = File.extname(f) | |
newname = f.gsub(/#{ext}$/i, '.x265.mkv') | |
input = shell_quote(f) | |
output = shell_quote(newname) | |
command = ['ffmpeg', '-y', '-i', input, '-c:v', 'libx265', '-c:a', 'copy', '-c:s', 'copy', output].join(' ') | |
logger.debug(command) | |
IO.popen(command, 'r') do |pipe| | |
logger.debug pipe.read | |
end | |
rescue | |
next | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment