This commit is contained in:
BoHung Chiu 2022-08-21 16:43:10 +08:00
parent c8b21416c1
commit 986cca8c5c
5 changed files with 60 additions and 23 deletions

View File

@ -151,8 +151,8 @@ class AdBannersController < ApplicationController
extra_before_html = "" extra_before_html = ""
extra_after_html = "" extra_after_html = ""
if has_jplayer if has_jplayer
extra_before_html = ActionController::Base.helpers.stylesheet_link_tag("banner/jplayer.blue.monday.min.css") extra_before_html = "\n" + ActionController::Base.helpers.stylesheet_link_tag("banner/jplayer.blue.monday.min.css") + "\n" +
extra_after_html = ActionController::Base.helpers.javascript_include_tag("banner/jquery.jplayer.min.js") ActionController::Base.helpers.javascript_include_tag("banner/jquery.jplayer.min.js") + "\n"
end end
base_image = adbanner.base_image.nil? ? 1 : adbanner.base_image base_image = adbanner.base_image.nil? ? 1 : adbanner.base_image
adbanner_title_escape = adbanner.title.to_s.gsub('"','&quot;') adbanner_title_escape = adbanner.title.to_s.gsub('"','&quot;')

View File

@ -8,7 +8,7 @@ class AdImage
mount_uploader :video_snapshot, BannerImageUploader mount_uploader :video_snapshot, BannerImageUploader
mount_uploader :video_file, BannerUploader mount_uploader :video_file, BannerUploader
mount_uploader :video_file_webm, BannerUploader mount_uploader :video_file_webm, BannerUploader
field :default_video_snapshot, type: Boolean, default: false
field :auto_convert_video, type: Boolean, default: false field :auto_convert_video, type: Boolean, default: false
field :title, type: String, localize: true field :title, type: String, localize: true
field :context, type: String, localize: true field :context, type: String, localize: true
@ -33,14 +33,39 @@ class AdImage
scope :can_display, ->{self.and(AdImage.unscoped.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,AdImage.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)} scope :can_display, ->{self.and(AdImage.unscoped.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,AdImage.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
scope :is_expired, ->{self.and(AdImage.unscoped.or({:deadline.lte=>Time.now}).selector)} scope :is_expired, ->{self.and(AdImage.unscoped.or({:deadline.lte=>Time.now}).selector)}
scope :not_expired, ->{self.and(AdImage.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)} scope :not_expired, ->{self.and(AdImage.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
before_save do
after_save do unless @skip_callback
if (self.video_file_changed? || self.auto_convert_video_changed?) && self.auto_convert_video if self.video_snapshot_changed?
Thread.new do self.default_video_snapshot = false
self.generate_webm
end end
end end
end end
after_save do
unless @skip_callback
if (self.video_file_changed? || self.auto_convert_video_changed?) && self.auto_convert_video
Thread.new do
self.generate_webm
end
end
if self.video_file? && self.video_file_changed? && (!(self.video_snapshot?) || self.default_video_snapshot)
Thread.new do
self.generate_video_snapshot
end
end
end
end
def generate_video_snapshot
if self.video_file?
self.default_video_snapshot = true
self[:video_snapshot] = File.basename(self.video_file.file.path).split(/\.[^.]+$/)[0] + ".jpg"
FileUtils.mkdir_p(File.dirname(self.video_snapshot.file.path))
system("tmp/ffmpeg/ffmpeg -i #{self.video_file.file.path} -vframes 1 #{self.video_snapshot.file.path}")
@skip_callback = true
self.save
@skip_callback = false
true
end
end
def generate_webm def generate_webm
video_path = self.video_file.path rescue nil video_path = self.video_file.path rescue nil
if !video_path.blank? if !video_path.blank?
@ -49,7 +74,10 @@ class AdImage
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}") 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 if flag
self.video_file_webm = File.open(video_webm) self.video_file_webm = File.open(video_webm)
@skip_callback = true
self.save self.save
@skip_callback = false
true
else else
puts "generate webm failed" puts "generate webm failed"
end end

View File

@ -66,7 +66,7 @@
<% end %> <% end %>
</td> </td>
<td> <td>
<%= image.title rescue "" %> <%= image.title.to_s.html_safe rescue "" %>
<% if can_edit_or_delete?(@banner) %> <% if can_edit_or_delete?(@banner) %>
<div class="quick-edit"> <div class="quick-edit">
<ul class="nav nav-pills"> <ul class="nav nav-pills">

View File

@ -209,14 +209,15 @@
}) })
if (typeof(default_video_data)=='undefined'){ if (typeof(default_video_data)=='undefined'){
var default_video_data = { var default_video_data = {
cssSelectorAncestor: "#jp_container_<%= i %>",
ready: function () { ready: function () {
$(this).jPlayer("setMedia", { $(this).jPlayer("setMedia", {
title: "Big Buck Bunny Trailer", title: "Big Buck Bunny Trailer",
m4v: "http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v", m4v: "http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
ogv: "http://www.jplayer.org/video/ogv/Big_Buck_Bunny_Trailer.ogv", ogv: "http://www.jplayer.org/video/ogv/Big_Buck_Bunny_Trailer.ogv",
webmv: "http://www.jplayer.org/video/webm/Big_Buck_Bunny_Trailer.webm", webmv: "http://www.jplayer.org/video/webm/Big_Buck_Bunny_Trailer.webm",
poster: "http://www.jplayer.org/video/poster/Big_Buck_Bunny_Trailer_480x270.png" poster: "http://www.jplayer.org/video/poster/Big_Buck_Bunny_Trailer_480x270.png"
}) })
}, },
play: function() { // To avoid multiple jPlayers playing together. play: function() { // To avoid multiple jPlayers playing together.
play_<%= i %>_flag = true; play_<%= i %>_flag = true;
@ -365,7 +366,8 @@
//jPlayer_<%= i %>_data["autoPlay"] = <%= @autoplay_video == true %>; //jPlayer_<%= i %>_data["autoPlay"] = <%= @autoplay_video == true %>;
jPlayer_<%= i %>["ready"] = function () { jPlayer_<%= i %>["ready"] = function () {
$(this).jPlayer("setMedia", jPlayer_<%= i %>_data); $(this).jPlayer("setMedia", jPlayer_<%= i %>_data);
if ($(this).parents(".ba-banner-widget-youtube").eq(0).find(".w-ba-banner__slide").index($(this).parents(".w-ba-banner__slide").eq(0))==0){ var slides = $(this).parents('.cycle-slideshow').data('cycle.opts').slides;
if (slides && slides.index($(this).parents(".w-ba-banner__slide").eq(0))==0){
<% if @autoplay_video == true %> <% if @autoplay_video == true %>
var $video_ele = $(this).find('video'); var $video_ele = $(this).find('video');
if ($video_ele.length>0){ if ($video_ele.length>0){

View File

@ -3,12 +3,19 @@ require 'fileutils'
get 'ad_banners/widget' get 'ad_banners/widget'
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Thread.new do Thread.new do
ad_images = AdImage.where(:video_file.ne=>nil,:video_snapshot=>nil).to_a s = Site.first
ad_images.each do |ad_image| update_flag = s.respond_to?(:tmp_flags)
ad_image[:video_snapshot] = File.basename(ad_image.video_file.file.path).split(/\.[^.]+$/)[0] + ".jpg" need_update = !update_flag || !(s.tmp_flags.include?('adf1'))
FileUtils.mkdir_p(File.dirname(ad_image.video_snapshot.file.path)) if need_update
system("tmp/ffmpeg/ffmpeg -i #{ad_image.video_file.file.path} -vframes 1 #{ad_image.video_snapshot.file.path}") ad_images = AdImage.where(:video_file.ne=>nil,:video_snapshot=>nil).to_a
ad_image.save ad_images.each do |ad_image|
ad_image.generate_video_snapshot
end
if update_flag
s = OrbitHelper::SharedHash['current_site']['site'] rescue Site.first
s.tmp_flags << 'adf1'
s.save
end
end end
end end
end end