Skip to content

Instantly share code, notes, and snippets.

@paradox460
Last active December 2, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paradox460/9218bd2e98f5ffc256fb9cb0aab63f48 to your computer and use it in GitHub Desktop.
Save paradox460/9218bd2e98f5ffc256fb9cb0aab63f48 to your computer and use it in GitHub Desktop.
#! /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
#! /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