-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcode.rb
executable file
·206 lines (190 loc) · 5.77 KB
/
transcode.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env ruby
require 'open3'
FFMPEG = "ffmpeg"
filename = ARGV[0]
filename_no_ext = filename.gsub(/\.[a-zA-Z0-9]+$/, '')
out_name = filename + ".webm"
ARGV.shift
dry_run = false
cuda = false
one_pass = false
banner = true
if ARGV.include?( "--dry-run") || ARGV.include?("-d")
dry_run = true
end
if ARGV.include?( "--cuda") || ARGV.include?("-c")
cuda = true
end
if ARGV.include?("-1")
one_pass = true
end
if ARGV.include?("-nb") || ARGV.include?("--no-banner")
banner = false
end
# get stream data:
# see http://web.archive.org/web/20180501034714/http://blog.honeybadger.io/capturing-stdout-stderr-from-shell-commands-via-ruby/
raw_stream_data, _, status = Open3.capture3("ffprobe -i '#{filename}' -show_streams")
## Dump ffmpeg output:
#print "\n\n"
#
#print raw_stream_data
#
#print "\n\n"
# Parse stream data into a map
counter = -1
streams = []
for line in raw_stream_data.split("\n")
if line.strip == '[STREAM]' then
counter += 1
streams[counter] = {}
elsif line.strip != '[\STREAM]'
sp = line.split("=", 2)
streams[counter][sp[0]] = sp[1]
end
end
# Parse maps
used_streams = []
videoNum = -1
audioNum = -1
subNum = -1
for stream in streams
index = stream["index"].to_i
if index == nil then
raise "No index for stream: #{stream}"
end
case stream["codec_type"]
when "video"
videoNum += 1
used_streams[index] = {:type => :video,
:vindex => videoNum}
when "audio"
# some movies have weird sub tracks that are
# quite needed even for english, see "Snowpiercer",
# so we're not going to attempt to pick and
# choose audio tracks
audioNum += 1
if streams.select { |elem| elem["TAG:language"] == stream["TAG:language"] and
elem["TAG:title"] == stream["TAG:title"]}
.map { |elem| elem["channels"].to_i }.max > stream["channels"].to_i then
# ignore it
else
used_streams[index] = {:type => :audio,
:channels => stream["channels"].to_i,
:lang => stream["TAG:language"],
:title => stream["TAG:title"],
:aindex => audioNum}
end
when "subtitle"
subNum += 1
# Make sure that the subtitles can actually be converted
# See this bull for why: http://web.archive.org/web/20180505045227/https://stackoverflow.com/questions/36326790/cant-change-video-subtitles-codec-using-ffmpeg
checkcmd = "timeout 0.5 #{FFMPEG} -hide_banner -i '#{filename}' -y -map 0:#{index} -c:s:0 webvtt -f webvtt /dev/null"
_, _, status = Open3.capture3(checkcmd)
if status.exitstatus == 0
used_streams[index] = {:type => :sub,
:sindex => subNum,
:lang => stream["TAG:language"],
:title => stream["TAG:title"] }
end
# Dump any other crap in the container file
end
end
puts "kept streams: #{used_streams}"
# Build up ffmpeg commands
# http://web.archive.org/web/20180501174740/https://trac.ffmpeg.org/wiki/Map
maps_1 = ""
copies_1 = []
maps_2 = ""
copies_2 = []
videoNum = -1
audioNum = -1
subNum = -1
def audioDefaultOptions(anum, stream, forceStereo)
case stream[:channels]
when 2
aProfile = "stereo"
when 3
aProfile = "2.1 channel"
when 6
aProfile = "5.1 channel"
when 8
aProfile = "7.1 channel"
else
aProfile = "#{stream[:channels]} channel"
end
if forceStereo
aProfile = "stereo"
end
if stream[:title].nil?
title = aProfile
else
title = "#{stream[:title]}: #{aProfile}"
end
"-c:a:#{anum} libopus -filter:a:#{anum} loudnorm -af:a:#{anum} aformat=channel_layouts=\"7.1|5.1|stereo\" -b:a:#{anum} 64k -metadata:s:a:#{anum} title='#{title}'"
end
VIDEO_CODEC = "libvpx-vp9"
used_streams.each_with_index do |stream, ii|
maps_2 += " -map 0:#{ii}"
case stream[:type]
when :video
videoNum += 1
maps_1 += " -map 0:#{ii}"
short = "-c:v:#{videoNum} #{VIDEO_CODEC} -crf:v:#{videoNum} 20 -pix_fmt:v:#{videoNum} yuv420p -threads 8 -b:v:#{videoNum} 0 "
copies_1.push short + "-speed 4"
copies_2.push short + "-speed 3 -auto-alt-ref 1 -lag-in-frames 25"
when :audio
if stream[:channels] > 2
audioNum += 1
maps_2 += " -map 0:#{ii}"
copies_2.push audioDefaultOptions(audioNum, stream, true) + " -ac:a:#{audioNum} 2"
end
audioNum += 1
copies_2.push audioDefaultOptions(audioNum, stream, false)
when :sub
subNum += 1
copies_2.push "-c:s:#{subNum} webvtt -metadata:s:a:#{subNum} title='#{stream[:title]}'"
end
end
FFMPEG_OPTIONS = "-i '#{filename}' -f webm"
if cuda
FFMPEG_OPTIONS = " -hwaccel cuvid " + FFMPEG_OPTIONS
end
if not banner
FFMPEG_OPTIONS = " -hide_banner " + FFMPEG_OPTIONS
end
if ! one_pass
pass2 = "-pass 2"
else
pass2 = ""
end
command1 = "#{FFMPEG} #{FFMPEG_OPTIONS} -y -pass 1 #{maps_1} #{copies_1.join(" ")} /dev/null"
command2 = "#{FFMPEG} #{FFMPEG_OPTIONS} #{pass2} #{maps_2} #{copies_2.join(" ")} '#{out_name}'"
if dry_run
if not one_pass
puts command1
end
puts "\n"
puts command2
else
if not one_pass and not system(command1)
puts "First pass failed"
exit 1
elsif not system(command2)
puts "Second pass failed"
exit 1
else
puts "Transcode successful"
exit 0
end
end
#Example 1:
#ffmpeg -i $1 -c:v libvpx-vp9 -b:v 2M -pass 1 -c:a libopus -f webm /dev/null && \
# ffmpeg -i $1 -c:v libvpx-vp9 -b:v 2M -pass 2 -c:a libopus $(echo $1 | sed -E 's/\.[a-zA-Z0-9]+$//').webm
#Example 2:
#ffmpeg -y -i $1 -c:v libvpx-vp9 -pass 1 -b:v 1000K -threads 8 -speed 4 \
#-tile-columns 6 -frame-parallel 1 \
#-an -f webm /dev/null
#ffmpeg -i $1 -c:v libvpx-vp9 -pass 2 -b:v 2M -threads 8 -speed 1 \
# -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 \
# -c:a libopus -f webm \
# $(echo $1 | sed -E 's/\.[a-zA-Z0-9]+$//').webm