Class: Wco::NewsvideosController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Wco::NewsvideosController
- Defined in:
- app/controllers/wco/newsvideos_controller.rb
Instance Method Summary collapse
- #add_image_as_overlay ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #generate ⇒ Object
- #generate_all_audio ⇒ Object
- #generate_all_video ⇒ Object
- #generate_illustration ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #show ⇒ Object
- #split ⇒ Object
- #update ⇒ Object
Instance Method Details
#add_image_as_overlay ⇒ Object
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 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 9 def duration_sec = 3 @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :edit, @newsvideo workdir = Rails.root.join('tmp', @newsvideo.id.to_s) FileUtils.mkdir_p(workdir) w_px = @newsvideo.w_px h_px = @newsvideo.h_px fps = @newsvideo.fps input = workdir.join("overlay_#{@newsvideo.}.jpg") URI.open(params[:image_url]) do |src| File.binwrite(input, src.read) end image_path = workdir.join("overlay_#{@newsvideo.}_#{w_px}x#{h_px}.jpg") cmd = <<~CMD magick #{input} \ -resize "#{w_px}x#{h_px}^" \ -gravity center \ -crop "#{w_px}x#{h_px}+0+0" \ +repage \ -quality 95 \ #{image_path} CMD puts "+++ magick cmd:" puts cmd out = `#{cmd}` puts! out, 'out' video_path = workdir.join("overlay_#{@newsvideo.}.mp4") cmd = <<~CMD ffmpeg -y \ -loop 1 \ -i #{image_path} \ -t #{duration_sec} \ -r #{fps} \ -c:v libx264 \ -pix_fmt yuv420p \ -vf "scale=#{w_px}:#{h_px}" \ #{video_path} CMD puts "+++ ffmpeg cmd:" puts cmd out = `#{cmd}` puts! out, 'out' @video = Wco::Video.new name: "#{@newsvideo.title} overlay #{@newsvideo.}" @video.video = File.open(video_path) @video.thumb = File.open(image_path) @video. = [ Wco::Tag.illustration, Wco::Tag.find_or_create_by({ slug: @newsvideo.slug }) ] flag = @video.save if !flag puts "Could not create video:" puts @video.errors..join(", ") end @overlay = Wco::Newsoverlay.new({ duration_ms: duration_sec * 1000, newsvideo: @newsvideo, start_at_ms: @newsvideo., video: @video, }); if @overlay.save @newsvideo.update( next_overlay_ms: @newsvideo. + @overlay.duration_ms ) else puts "Could not create overlay:" puts @overlay.errors..join(", ") end render json: { status: :ok } end |
#create ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 84 def create params[:newsvideo][:tag_ids]&.delete '' @newsvideo = Wco::Newsvideo.new params[:newsvideo].permit! :create, @newsvideo @newsvideo. = current_profile if @newsvideo.save flash_notice "created newsvideo" else flash_alert "Cannot create newsvideo: #{@newsvideo.errors.}" end redirect_to action: 'index' end |
#destroy ⇒ Object
98 99 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 98 def destroy end |
#edit ⇒ Object
101 102 103 104 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 101 def edit @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :edit, @newsvideo end |
#generate ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 106 def generate @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :edit, @newsvideo Rails.env.production? ? Wco::NewsvideoGenerateJob.perform_async(@newsvideo.id.to_s) : Wco::NewsvideoGenerateJob.perform_sync( @newsvideo.id.to_s) render json: { status: :ok, message: 'Scheduled the generation' } end |
#generate_all_audio ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 117 def generate_all_audio @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :edit, @newsvideo cmd = "cd #{PUPPETEER_ROOT} ; yarn run newsvideo_generate_all_audio #{@newsvideo.id}" puts "+++ puppeteer cmd:" puts cmd out = `#{cmd}` puts! out, 'out' render json: { status: :ok } end |
#generate_all_video ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 130 def generate_all_video @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :edit, @newsvideo cmd = "cd #{PUPPETEER_ROOT} ; yarn run newsvideo_generate_all_video #{@newsvideo.id}" puts "+++ puppeteer cmd:" puts cmd out = `#{cmd}` puts! out, 'out' render json: { status: :ok } end |
#generate_illustration ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 143 def generate_illustration @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :edit, @newsvideo Rails.env.production? ? Wco::NewsvideoIllustrationJob.perform_async(@newsvideo.id.to_s, params[:image_url]) : Wco::NewsvideoIllustrationJob.perform_sync( @newsvideo.id.to_s, params[:image_url]) render json: { status: :ok, message: 'scheduled the run on pc-ai' } # redirect_to action: 'show', newsvideo_id: params[:id] end |
#index ⇒ Object
155 156 157 158 159 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 155 def index :index, Wco::Newsvideo @newsvideos = Wco::Newsvideo.all.order_by( created_at: :desc ) @newsvideos = @newsvideos.page( params[:newsvideos_page] ).per( current_profile.per_page ) end |
#new ⇒ Object
161 162 163 164 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 161 def new @newsvideo = Wco::Newsvideo.new :create, @newsvideo end |
#show ⇒ Object
166 167 168 169 170 171 172 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 166 def show @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :show, @newsvideo @newspartials = Wco::Newspartial.where( newsvideo_id: @newsvideo.id.to_s).includes(:video) @newsoverlays = @newsvideo. @skip_footer = true end |
#split ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 174 def split @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :edit, @newsvideo if Wco::Newspartial.where( newsvideo: @newsvideo).length > 0 # Wco::Newspartial.where( newsvideo: @newsvideo).each { |n| n.delete } flash_alert 'newspartials already exist for this newsvideo - cannot split.' redirect_to request.referrer return end @newsvideo.do_split flash_notice 'Done spliting the video.' redirect_to request.referrer end |
#update ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'app/controllers/wco/newsvideos_controller.rb', line 192 def update params[:newsvideo][:tag_ids]&.delete '' @newsvideo = Wco::Newsvideo.unscoped.find params[:id] :update, @newsvideo if @newsvideo.update params[:newsvideo].permit! flash_notice "updated newsvideo" else flash_alert "Cannot update newsvideo: #{@newsvideo.errors.}" end redirect_to action: 'index' end |