From c4e44e144272e124bbdbd291474fdc8d9aa0250e Mon Sep 17 00:00:00 2001 From: anke1460 Date: Mon, 9 Mar 2020 20:03:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AC=E7=A0=81=E8=BD=AC=E7=A0=81=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/libs/aliyun_vod/service/video_manage.rb | 14 ++++++++++++++ app/libs/aliyun_vod/service/video_process.rb | 16 ++++++++++++++++ app/models/video.rb | 3 +++ app/services/videos/batch_publish_service.rb | 8 ++++++++ app/services/videos/dispatch_callback_service.rb | 5 +++-- .../20200309114326_add_transcoded_to_video.rb | 5 +++++ lib/tasks/video_transcode.rake | 12 ++++++++++++ 7 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20200309114326_add_transcoded_to_video.rb create mode 100644 lib/tasks/video_transcode.rake diff --git a/app/libs/aliyun_vod/service/video_manage.rb b/app/libs/aliyun_vod/service/video_manage.rb index 02a4bac49..b00d86892 100644 --- a/app/libs/aliyun_vod/service/video_manage.rb +++ b/app/libs/aliyun_vod/service/video_manage.rb @@ -26,6 +26,20 @@ module AliyunVod::Service::VideoManage result end + # 读取视频编码格式 + def get_meta_code_info(video_id) + params = { + Action: 'GetMezzanineInfo', + VideoId: video_id, + AdditionType: 'video' + }.merge(base_params) + + result = request(:post, params) + result['Mezzanine']['VideoStreamList'][0]['CodecName'] + rescue => e + Rails.logger.info "读取视频编码信息失败: #{video_id}, #{e.message}" + end + # 删除视频信息 def delete_video(video_ids) params = { diff --git a/app/libs/aliyun_vod/service/video_process.rb b/app/libs/aliyun_vod/service/video_process.rb index eec029c20..61acc4051 100644 --- a/app/libs/aliyun_vod/service/video_process.rb +++ b/app/libs/aliyun_vod/service/video_process.rb @@ -14,4 +14,20 @@ module AliyunVod::Service::VideoProcess result end + + # 提交视频转码任务 + def submit_transcode_job(video_id, group_id, **opts) + params = { + Action: 'SubmitTranscodeJobs', + VideoId: video_id, + TemplateGroupId: group_id + }.merge(base_params) + params = opts.merge(params) + + result = request(:post, params) + + raise AliyunVod::Error, '提交视频转码作业失败' if result['TranscodeJobs'].blank? + + result + end end \ No newline at end of file diff --git a/app/models/video.rb b/app/models/video.rb index 6f5f79ca6..02cccd8f1 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,6 +1,9 @@ class Video < ApplicationRecord include AASM + # 标准视频转码组 + NORMAL_TRANSCODE_GROUP_ID = 'a0277c5c0c7458458e171b0cee6ebf5e' + belongs_to :user has_many :video_applies, dependent: :destroy diff --git a/app/services/videos/batch_publish_service.rb b/app/services/videos/batch_publish_service.rb index 0523097a2..c9b1b69c7 100644 --- a/app/services/videos/batch_publish_service.rb +++ b/app/services/videos/batch_publish_service.rb @@ -28,6 +28,14 @@ class Videos::BatchPublishService < ApplicationService if param[:course_id].present? video.status = "published" end + + # 标清转码为h264 + if AliyunVod::Service.get_meta_code_info(video.uuid).start_with?('h264', 'h265', 'flv') + video.transcoded = true + else + AliyunVod::Service.submit_transcode_job(video.uuid, Video::NORMAL_TRANSCODE_GROUP_ID) + end + video.save! if param[:course_id].present? diff --git a/app/services/videos/dispatch_callback_service.rb b/app/services/videos/dispatch_callback_service.rb index 5adebea73..e81a52d3a 100644 --- a/app/services/videos/dispatch_callback_service.rb +++ b/app/services/videos/dispatch_callback_service.rb @@ -15,14 +15,15 @@ class Videos::DispatchCallbackService < ApplicationService video.file_url = convert_https(params['FileUrl']) video.filesize = params['Size'] video.upload_success + video.play_url = video.file_url if video.transcoded #不需要转码时,则统一播放地址 video.save! when 'SnapshotComplete' then # 封面截图完成 return if video.cover_url.present? video.update!(cover_url: params['CoverUrl']) - when 'TranscodeComplete' then # 转码完成 + when 'StreamTranscodeComplete' then # 转码完成 return if video.play_url.present? - video.update!(play_url: params['FileUrl']) + video.update!(play_url: params['FileUrl'], transcoded: true) end rescue => ex diff --git a/db/migrate/20200309114326_add_transcoded_to_video.rb b/db/migrate/20200309114326_add_transcoded_to_video.rb new file mode 100644 index 000000000..31e2764db --- /dev/null +++ b/db/migrate/20200309114326_add_transcoded_to_video.rb @@ -0,0 +1,5 @@ +class AddTranscodedToVideo < ActiveRecord::Migration[5.2] + def change + add_column :videos, :transcoded, :boolean, default: false + end +end diff --git a/lib/tasks/video_transcode.rake b/lib/tasks/video_transcode.rake new file mode 100644 index 000000000..a595b77b0 --- /dev/null +++ b/lib/tasks/video_transcode.rake @@ -0,0 +1,12 @@ +#coding=utf-8 +namespace :video_transcode do + desc "视频转码成h264" + task :submit => :environment do + Video.find_each do |v| + if v.uuid && v.transcoded == false && AliyunVod::Service.get_meta_code_info(v.uuid).start_with?("h264", "h265", "flv") == false + p "--- Start submit video trans code #{v.uuid}" + AliyunVod::Service.submit_transcode_job(v.uuid, 'a0277c5c0c7458458e171b0cee6ebf5e') + end + end + end +end \ No newline at end of file