From 495cebdd257dc9f767a916fd466ab9a952e3c0d9 Mon Sep 17 00:00:00 2001 From: anke1460 Date: Thu, 19 Mar 2020 02:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E8=A7=86=E9=A2=91=E6=97=B6?= =?UTF-8?q?=E9=95=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/create_watch_video_service.rb | 7 ++++++- app/services/videos/batch_publish_service.rb | 13 ++++++++++--- db/migrate/20200318181442_add_duration_to_video.rb | 5 +++++ lib/tasks/get_video_data.rake | 13 +++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20200318181442_add_duration_to_video.rb diff --git a/app/services/create_watch_video_service.rb b/app/services/create_watch_video_service.rb index 4629d9c8e..4920374dc 100644 --- a/app/services/create_watch_video_service.rb +++ b/app/services/create_watch_video_service.rb @@ -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! diff --git a/app/services/videos/batch_publish_service.rb b/app/services/videos/batch_publish_service.rb index 553ffe4f2..1c3795102 100644 --- a/app/services/videos/batch_publish_service.rb +++ b/app/services/videos/batch_publish_service.rb @@ -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? diff --git a/db/migrate/20200318181442_add_duration_to_video.rb b/db/migrate/20200318181442_add_duration_to_video.rb new file mode 100644 index 000000000..caf3a70e9 --- /dev/null +++ b/db/migrate/20200318181442_add_duration_to_video.rb @@ -0,0 +1,5 @@ +class AddDurationToVideo < ActiveRecord::Migration[5.2] + def change + add_column :videos, :duration, :float, default: 0 + end +end diff --git a/lib/tasks/get_video_data.rake b/lib/tasks/get_video_data.rake index f6f85c7d5..3a86273ec 100644 --- a/lib/tasks/get_video_data.rake +++ b/lib/tasks/get_video_data.rake @@ -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 \ No newline at end of file