读取视频时长

courseware
anke1460 5 years ago
parent 67f27dda92
commit 495cebdd25

@ -50,7 +50,12 @@ class CreateWatchVideoService < ApplicationService
# 更新课程视频的时长及是否看完状态
watch_course_video.watch_duration = params[:watch_duration] if watch_course_video.watch_duration < params[:watch_duration]
if params[:ed].present? || (watch_course_video.duration >= 300 && (watch_course_video.duration - params[:point].to_i) <= 20)
watch_course_video.is_finished = watch_course_video.total_duration >= watch_course_video.duration
video_duration = watch_video_history.video.duration.to_i
if video_duration > 0
watch_course_video.is_finished = watch_course_video.total_duration >= video_duration
else
watch_course_video.is_finished = watch_course_video.total_duration >= watch_course_video.duration
end
end
end
watch_course_video.save!

@ -31,16 +31,23 @@ class Videos::BatchPublishService < ApplicationService
# 非MP4 H264编码的都转码
code_info = AliyunVod::Service.get_meta_code_info(video.uuid)
result = AliyunVod::Service.get_play_info(video.uuid) rescue nil
Rails.logger.info("code_info: #{code_info[:format]}, #{code_info[:codecnamne]}")
if code_info[:format] == "mp4" && code_info[:codecnamne].present? && code_info[:codecnamne].start_with?('h264')
video.transcoded = true
result = AliyunVod::Service.get_play_info(video.uuid) rescue nil
play_url = result['PlayInfoList']['PlayInfo'].first['PlayURL'] if result.present?
video.play_url = play_url
if result.present? && result['PlayInfoList']['PlayInfo'].first['PlayURL']
play_url = result['PlayInfoList']['PlayInfo'].first['PlayURL']
video.play_url = play_url
end
else
AliyunVod::Service.submit_transcode_job(video.uuid, Video::NORMAL_TRANSCODE_GROUP_ID)
end
if result.present?
video.duration = result["VideoBase"]["Duration"] if result["VideoBase"]["Duration"]
end
video.save!
if param[:course_id].present?

@ -0,0 +1,5 @@
class AddDurationToVideo < ActiveRecord::Migration[5.2]
def change
add_column :videos, :duration, :float, default: 0
end
end

@ -74,4 +74,17 @@ namespace :video do
end
end
end
task :set_duration => :environment do
videos = Video.published.where("duration = 0")
videos.find_each do |v|
result = AliyunVod::Service.get_play_info(v.uuid)
if result.present? && result["VideoBase"]["Duration"].present?
p '-----#{v.id} , #{result["VideoBase"]["Duration"]]}'
video.update(duration: result["VideoBase"]["Duration"])
end
end
end
end
Loading…
Cancel
Save