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
|
# File 'app/models/wco/newsvideo.rb', line 70
def generate
@newsvideo = self
workdir = Rails.root.join('tmp', @newsvideo.id.to_s)
partials = @newsvideo.newspartials.order_by(slug: :asc).to_a
cmd = "cd #{Rails.root.join('tmp')} ; rm -rf #{@newsvideo.id} ; mkdir -p #{@newsvideo.id} ; cd #{@newsvideo.id} ; "
partials.each_with_index do |_part, idx|
cmd = "#{cmd} echo \"file 'synced_#{idx}.mp4'\" >> syncedlist.txt ; "
end
puts "+++ config cmd:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
cmd = "cd #{workdir} ; "
partials.each_with_index do |part, idx|
cmd = "#{cmd} wget -nc -O newspartial_#{idx}.webm #{part.video.video.url} ; "
cmd = "#{cmd} [ -f newspartial_#{idx}.mp4 ] || ffmpeg -y -i newspartial_#{idx}.webm -r 24 -pix_fmt yuv420p newspartial_#{idx}.mp4 ; "
cmd = "#{cmd} wget -nc -O newspartial_#{idx}.wav #{part.audio.url} ; "
end
puts "+++ base files cmd:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
partials.each_with_index do |_part, idx|
cmd = <<~AOL
cd #{workdir} ;
rm -f synced_#{idx}.mp4 ;
ffmpeg -y -i newspartial_#{idx}.mp4 -i newspartial_#{idx}.wav \
-map 0:v:0 \
-map 1:a:0 \
-c:v libx264 \
-pix_fmt yuv420p \
-c:a aac \
-shortest \
synced_#{idx}.mp4 ;
AOL
puts "+++ per-partial #{idx} sync cmd:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
end
cmd = <<~AOL
cd #{workdir} ;
rm -f combined_base.mp4 ;
ffmpeg -y -f concat -safe 0 -i syncedlist.txt -c:v libx264 -pix_fmt yuv420p \
-c:a aac \
-vsync cfr \
-af "aresample=async=1:first_pts=0" \
combined_base.mp4 ;
AOL
puts "+++ synced concat cmd:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
if @newsvideo.newsoverlays.length > 0
cmd = "cd #{Rails.root.join('tmp', @newsvideo.id)} ; "
@newsvideo.newsoverlays.each_with_index do |overlay, idx|
cmd = "#{cmd} wget -nc -O overlay_#{idx}.mp4 #{overlay.video.video.url} ; "
end
puts "+++ overlays cmd:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
nn = @newsvideo.newsoverlays.map { |ol| ol.start_at_ms }
puts! nn, 'nn'
ffmpeg_cmd = [ "ffmpeg -y -i combined_base.mp4 \\" ]
nn.each_with_index do |ms, idx|
ffmpeg_cmd.push " -i overlay_#{idx}.mp4 \\"
end
ffmpeg_cmd.push "-filter_complex \"\\"
nn.each_with_index do |ms, idx|
ffmpeg_cmd.push "[#{idx+1}:v]setpts=PTS-STARTPTS+#{ms.to_f/1000}/TB[v#{idx+1}]; \\"
end
curr_s = "0:v"
n = nil
nn.each_with_index do |ms, idx|
n = idx+1
ffmpeg_cmd.push "[#{curr_s}][v#{n}]overlay=0:0:eof_action=pass[tmp#{n}]#{idx+1<nn.length ? ';' : ''} \\"
curr_s = "tmp#{n}"
end
ffmpeg_cmd.push " \" -map \"[#{curr_s}]\" -map 0:a? -c:v libx264 -c:a copy combined_fin.mp4 "
ffmpeg_cmd = ffmpeg_cmd.join("\n")
puts "+++ ffmpeg_cmd:"
puts ffmpeg_cmd
cmd = <<~AOL
cd #{Rails.root.join('tmp', @newsvideo.id)} ;
rm -f combined_fin.mp4 ;
#{ffmpeg_cmd} ;
AOL
puts "+++ ffmpeg cmd 2:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
else
cmd = `cd #{workdir} ; mv combined_base.mp4 combined_fin.mp4`
end
@video = Wco::Video.new name: @newsvideo.title
video_path = Rails.root.join("tmp", @newsvideo.id, "combined_fin.mp4")
@video.video = File.open(video_path)
flag = @video.save
if !flag
puts "Could not create video:"
puts @video.errors.full_messages.join(", ")
end
@newsvideo.update( generated_video: @video )
end
|