Class: Wco::Newsvideo

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps, Utils
Defined in:
app/models/wco/newsvideo.rb

Constant Summary collapse

PAGE_PARAM_NAME =
'newsvideos_page'

Instance Method Summary collapse

Methods included from Utils

#export, included

Instance Method Details

#do_splitObject



54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/wco/newsvideo.rb', line 54

def do_split
  @newsvideo = self

  sentences = PragmaticSegmenter::Segmenter.new(text: @newsvideo.body).segment
  phrases = sentences_to_phrases(sentences)

  phrases.each do |phrase|
    newspartial = Wco::Newspartial.new body: phrase, newsvideo: @newsvideo
    newspartial.save!
  end
end

#generateObject



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
# File 'app/models/wco/newsvideo.rb', line 67

def generate
  @newsvideo = self

  ## put together config, first thing
  cmd = "cd #{Rails.root.join('tmp')} ; rm -rf #{@newsvideo.id} ; mkdir -p #{@newsvideo.id} ; cd #{@newsvideo.id} ; "
  @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'

  ## get base files locally
  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'

  ## get overlays
  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'

  ## video concat
  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 concat
  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'

  ## combine base
  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'

  ##  combine overlays
  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

  # combine overlays 2
  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'

  ## upload the video.
  @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

#nameObject



14
# File 'app/models/wco/newsvideo.rb', line 14

def name ; title ; end

#newsoverlaysObject



50
51
52
# File 'app/models/wco/newsvideo.rb', line 50

def newsoverlays
  Wco::Newsoverlay.where( newsvideo_id: self.id )
end

#newspartialsObject



40
41
42
# File 'app/models/wco/newsvideo.rb', line 40

def newspartials
  Wco::Newspartial.where( newsvideo_id: self.id )
end

#sentences_to_phrases(sentences) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/models/wco/newsvideo.rb', line 184

def sentences_to_phrases sentences
  phrases = []
  current_phrase = []

  current_word_count = 0

  sentences.each do |sentence|
    words_in_sentence = sentence.split.size

    # If adding this sentence exceeds the limit, start a new phrase
    if current_word_count + words_in_sentence > Wco::Newspartial::MAX_WORDS
      phrases << current_phrase.join(" ")
      current_phrase = []
      current_word_count = 0
    end

    current_phrase << sentence
    current_word_count += words_in_sentence
  end

  # Add the last phrase if any
  phrases << current_phrase.join(" ") unless current_phrase.empty?
end

#to_sObject



15
16
17
# File 'app/models/wco/newsvideo.rb', line 15

def to_s
  title
end