-
Notifications
You must be signed in to change notification settings - Fork 2
/
hb.rb
executable file
·127 lines (115 loc) · 3.96 KB
/
hb.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env ruby
require 'logger'
require File.join(File.dirname(File.absolute_path(__FILE__)), "lib", "hb_lib.rb")
include HandbrakeCLI
puts "#{File.basename($0)} Copyright (C) 2019 AlBundy
This program comes with ABSOLUTELY NO WARRANTY; for details see LICENSE.txt.
This is free software, and you are welcome to redistribute it under certain conditions.
For questions, feature-requests etc. visit: https://forum.handbrake.fr/viewtopic.php?f=10&t=26163
"
options = HandbrakeCLI::HBOptions::parseArgs(ARGV)
# initialize logger
log_device = nil
if options.logfile.nil?
log_device = Tools::Loggers::DefaultLogDev.new(STDOUT)
else
log_device = Tools::Loggers::DefaultLogDev.new(STDOUT, File.open(options.logfile, options.logOverride ? "w" : "a"))
end
HandbrakeCLI::logger = Tools::Loggers.createLogger(nil, log_device)
if options.verbose or options.debug
HandbrakeCLI::logger.level = Logger::DEBUG
else
HandbrakeCLI::logger.level = Logger::INFO
end
titleMatcher = PosMatcher.new(options.titles)
audioMatcher = LangMatcher.new(options.languages)
audioMatcher.onlyFirstPerAllowedValue = options.onlyFirstTrackPerLanguage
audioMatcher.skipCommentaries = options.skipCommentaries
subtitleMatcher = LangMatcher.new(options.subtitles)
subtitleMatcher.skipCommentaries = options.skipCommentaries
subtitleMatcher.skipForced = options.skipForced
# collect all inputs
inputs = [options.input]
inputs += options.argv if not options.argv.empty?
begin
inout = []
current_loop = options.loops
while current_loop != 0
inputs.each do |input|
#input = File.expand_path(input)
if input.include?("*")
i_list = Dir[input]
if i_list.empty?
puts "found no files for pattern #{input}"
sleep 1
next
end
else
i_list = [input]
end
i_list.each do |i|
opts = options.dup
unless Tools::FileTool::wait_for(i, options.inputWaitLoops, 2) { |loop, max, age|
if loop == max
puts "waiting for #{opts.input}..."
HandbrakeCLI::logger.warn("modification time of #{i} is in future") if age < 0
end
HandbrakeCLI::logger.debug("input=#{i}, loop=#{loop}, max=#{max}, age=#{age}")
}
puts "#{opts.input} does not exist"
next
end
opts.input = File.expand_path(i)
hb_input = HBConvertInput.new(opts.input)
results = Handbrake::convert(opts, titleMatcher, audioMatcher, subtitleMatcher)
inout << [hb_input, results]
end
end
current_loop -= 1
end
rescue Interrupt
puts "CTRL-C detected - stopping conversion"
end
# default overview
if options.logOverview.nil?
overview = nil
else
overview = File.open(options.logOverview, options.logOverride ? "w" : "a")
end
write_overview = lambda{|msg|
HandbrakeCLI::logger.info("#{msg}")
overview.puts("#{msg}") unless overview.nil?
}
begin
write_overview.call("overview")
inout.each do |input,results|
write_overview.call("input: #{input}")
if results.empty?
write_overview.call(" no outputs")
else
results.each do |result|
write_overview.call(" #{result.file} (#{result.fileSize})")
unless result.output.nil?
result.output.titles.each do |title|
write_overview.call(" title #{title.pos} #{title.duration}, #{title.size}, #{title.fps}fps")
unless title.audioTracks.empty?
write_overview.call(" audio-tracks:")
title.audioTracks.each do |t|
write_overview.call(" track #{t.pos}. #{t.descr} (#{t.lang})")
end
end
unless title.subtitles.empty?
write_overview.call(" subtitles:")
title.subtitles.each do |t|
write_overview.call(" track #{t.pos}. #{t.descr} (#{t.lang})")
end
end
end
end
end
end
end
ensure
overview.close unless overview.nil?
end
HandbrakeCLI::logger.close()