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
|
# File 'app/models/wco/newsvideo.rb', line 53
def generate
@newsvideo = self
cmd = "cd #{Rails.root.join('tmp')} ; mkdir -p #{@newsvideo.id} ; cd #{@newsvideo.id} ; rm -f videolist.txt audiolist.txt ; "
@newsvideo.newspartials.each_with_index do |part, idx|
cmd = "#{cmd} echo \"file 'newspartial_#{idx}.mp4' \" >> videolist.txt ; "
cmd = "#{cmd} echo \"file 'newspartial_#{idx}.wav' \" >> audiolist.txt ; "
end
puts "+++ config cmd:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
cmd = "cd #{Rails.root.join('tmp', @newsvideo.id)} ; "
@newsvideo.newspartials.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 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'
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'
cmd = <<AOL
cd #{Rails.root.join('tmp', @newsvideo.id)} ;
rm -f video_concat.mp4 ;
ffmpeg -y -f concat -safe 0 -i videolist.txt -c copy video_concat.mp4 ;
AOL
puts "+++ video concat cmd:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
audio_filenames = (0...@newsvideo.newspartials.length).map { |i| "newspartial_#{i}.wav" }.join("|")
cmd = <<AOL
cd #{Rails.root.join('tmp', @newsvideo.id)} ;
rm -f audio_concat.wav ;
ffmpeg -y -f concat -safe 0 -i audiolist.txt -c copy audio_concat.wav ;
AOL
puts "+++ audio concat cmd:"
puts cmd
out = `#{cmd}`
puts! out, 'out'
cmd = <<AOL
cd #{Rails.root.join('tmp', @newsvideo.id)} ;
rm -f output.mp4 ;
ffmpeg -y -i video_concat.mp4 -i audio_concat.wav -c:v copy -c:a aac combined_base.mp4 ;
AOL
puts "+++ combine base 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'
@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
end
|