From 64e26d9e2ed3114bd68f1d1d82e80aca99f25e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Fri, 22 Oct 2021 23:31:10 +0800 Subject: [PATCH] add auto convert video feature --- ad_banner.gemspec | 22 +++++++++++++++++++ app/controllers/ad_banners_controller.rb | 5 +++-- app/models/ad_image.rb | 24 +++++++++++++++++++++ app/views/admin/ad_images/_form.html.erb | 14 ++++++++++-- app/views/admin/ad_images/_jplayer.html.erb | 4 ++++ config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + 7 files changed, 67 insertions(+), 4 deletions(-) diff --git a/ad_banner.gemspec b/ad_banner.gemspec index 66e00d8..28c86bd 100644 --- a/ad_banner.gemspec +++ b/ad_banner.gemspec @@ -16,6 +16,28 @@ all_template.each do |folder| end end +#download ffmpeg +begin + destination = ENV['PWD']+'/tmp/ffmpeg' + if Dir[destination].length==0 + require 'open-uri' + download = open('https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2/ffmpeg-4.2-linux-64.zip') + save_zip_name = "#{ENV['PWD']}/tmp/ffmpeg-4.2-linux-64.zip" + IO.copy_stream(download, save_zip_name) + require 'zip' + FileUtils.mkdir_p(destination) + Zip::File.open(save_zip_name) do |zip_file| + zip_file.each do |f| + fpath = File.join(destination, f.name) + zip_file.extract(f, fpath) unless File.exist?(fpath) + end + end + end +rescue => e + FileUtils.rmdir(destination) rescue nil + puts ["download ffmpeg failed",e] +end +#./ffmpeg -i 2.mp4 -c:v libvpx-vp9 -crf 35 -b:v 0 -b:a 96k -c:a libopus -filter:v fps=20 output.webm -cpu-used 4 # Describe your gem and declare its dependencies: Gem::Specification.new do |s| s.name = "ad_banner" diff --git a/app/controllers/ad_banners_controller.rb b/app/controllers/ad_banners_controller.rb index 8b0d14d..4eb82fd 100644 --- a/app/controllers/ad_banners_controller.rb +++ b/app/controllers/ad_banners_controller.rb @@ -99,7 +99,8 @@ class AdBannersController < ApplicationController " elsif ad_b.exchange_item == "3" klass = (i == 0 ? "active" : "") - video_url = ad_b.video_file.url + video_url = ad_b.video_file.url rescue nil + video_webm_url = ad_b.video_file_webm.url rescue nil title = (ad_b.title.blank? ? File.basename(video_file) : ad_b.title) image_html = "
- #{ render_to_string(partial: "admin/ad_images/jplayer",locals:{i: i,file_name: title,file_url: video_url,:@autoplay_video=>(@autoplay_video && i == 0),:@apply_autoplay_script=>@autoplay_video,:@hide_video_tools=>@hide_video_tools}, layout: false).to_str } + #{ render_to_string(partial: "admin/ad_images/jplayer",locals:{i: i,file_name: title,file_url: video_url,file_webm_url: video_webm_url,:@autoplay_video=>(@autoplay_video && i == 0),:@apply_autoplay_script=>@autoplay_video,:@hide_video_tools=>@hide_video_tools}, layout: false).to_str }
" has_jplayer = true elsif ad_b.exchange_item == "2" diff --git a/app/models/ad_image.rb b/app/models/ad_image.rb index 1801dac..911cf6e 100644 --- a/app/models/ad_image.rb +++ b/app/models/ad_image.rb @@ -6,7 +6,9 @@ class AdImage mount_uploader :file, ImageUploader mount_uploader :video_file, AssetUploader + mount_uploader :video_file_webm, AssetUploader + field :auto_convert_video, type: Boolean, default: false field :title, type: String, localize: true field :context, type: String, localize: true field :weight, type: Integer, default: 1 @@ -31,6 +33,28 @@ class AdImage scope :is_expired, ->{self.and(AdImage.or({:deadline.lte=>Time.now}).selector)} scope :not_expired, ->{self.and(AdImage.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)} + after_save do + if (self.video_file_changed? || self.auto_convert_video_changed?) && self.auto_convert_video + Thread.new do + self.generate_webm + end + end + end + def generate_webm + video_path = self.video_file.path rescue nil + if !video_path.blank? + video_webm = video_path.split('.')[0...-1].join('.')+".webm" + core_num = [`cat /proc/cpuinfo | grep processor | wc -l`.to_i/2,1].max + flag = system("tmp/ffmpeg/ffmpeg -i #{video_path} -c:v libvpx-vp9 -crf 35 -b:v 0 -b:a 96k -c:a libopus -filter:v fps=20 #{video_webm} -cpu-used #{core_num}") + if flag + self.video_file_webm = File.open(video_webm) + self.save + else + puts "generate webm failed" + end + end + end + def expired? self.deadline
diff --git a/app/views/admin/ad_images/_jplayer.html.erb b/app/views/admin/ad_images/_jplayer.html.erb index 2f4dbaf..c56066b 100644 --- a/app/views/admin/ad_images/_jplayer.html.erb +++ b/app/views/admin/ad_images/_jplayer.html.erb @@ -329,6 +329,10 @@ }; var jPlayer_<%= i %> = $.extend({},default_video_data); jPlayer_<%= i %>_data[jPlayer_<%= i %>_type] = "<%= file_url %>"; + <% if file_webm_url %> + jPlayer_<%= i %>_data['webmv'] = "<%= file_webm_url %>"; + <% end %> + //jPlayer_<%= i %>_data['poster'] = "<%= file_url %>"; //jPlayer_<%= i %>_data["autoPlay"] = <%= @autoplay_video == true %>; jPlayer_<%= i %>["ready"] = function () { $(this).jPlayer("setMedia", jPlayer_<%= i %>_data); diff --git a/config/locales/en.yml b/config/locales/en.yml index e4d907f..65f086b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,6 +1,7 @@ en: ad_banner: + auto_convert_video: Auto Convert Video to webm(reduce usage for network traffic) autoplay_video: "Autoplay Video(play mute)" hide_video_tools: Hide video tools select_video: Select Video diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index ba1be18..a8ff6ac 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,6 +1,7 @@ zh_tw: ad_banner: + auto_convert_video: 自動轉換成webm格式(節省播放流量) autoplay_video: "自動播放影片(會自動靜音)" hide_video_tools: 隱藏影片工具 select_video: 選擇影片