Skip to content

Instantly share code, notes, and snippets.

@arion
Created April 15, 2012 18:28
Show Gist options
  • Save arion/2394254 to your computer and use it in GitHub Desktop.
Save arion/2394254 to your computer and use it in GitHub Desktop.
hardsub two subtitle file in one avi
#!/usr/bin/env ruby
require "rubygems"
require "pry"
$DEBUG = false
def hardsub(source_file, sub_file, sub_encoding, position, result_file)
command = %Q{ mencoder -oac mp3lame -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=2000 #{source_file} \
-subwidth 75 -subcp #{sub_encoding} -subfont-text-scale 3 -sub #{sub_file} \
-o #{result_file} -subpos #{position}}
command += ' > /dev/null' unless $DEBUG
puts "[DEBUG] run command: #{command}" if $DEBUG
result = system(command)
raise '[ERROR] command failed' if (!result || $?.to_i > 0)
end
def hardsub_top_and_bottom(source_file, top_sub_file, bottom_sub_file, sub_encoding, result_path)
file_name = 'hardsub_' + File.basename(source_file)
result_path = File.dirname(source_file) unless result_path
top_file = File.join('/', 'tmp', file_name)
hardsub(source_file, top_sub_file, sub_encoding, 0, top_file)
hardsub(top_file, bottom_sub_file, sub_encoding, 100, File.join(result_path, file_name))
end
def hardsub_all_files_from_folders(video_folder, top_sub_folder, bottom_sub_folder, result_folder = nil, sub_encoding = 'cp1251')
result_folder = video_folder unless result_folder
Dir[File.join(video_folder, '*.avi')].each do |video_file|
filename = File.basename(video_file).gsub(File.extname(video_file), '.srt')
top_sub_file = File.join(top_sub_folder, filename)
bottom_sub_file = File.join(bottom_sub_folder, filename)
if !File.exist?(top_sub_file) || !File.exist?(bottom_sub_file)
raise "[ERROR] Can't fine subtitle #{top_sub_file} or #{top_sub_file}"
end
puts "[INFO] hardsub file #{video_file} with top sub #{top_sub_file} and bottom sub #{bottom_sub_file} to #{result_folder} with encoding #{sub_encoding}"
hardsub_top_and_bottom(
video_file,
top_sub_file,
bottom_sub_file,
sub_encoding,
result_folder
)
end
end
if ARGV.length < 3
puts "Dude, not the right number of arguments."
puts "Usage: ruby hardsub.rb path/to/video/files path/to/top_sub/files path/to/bottom_sub/files [path/to/result/files] [sub_encoding]"
exit
else
hardsub_all_files_from_folders(*ARGV)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment