本文主要是介绍ruby on rails aliyun视频点播播放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
签名请看: https://blog.csdn.net/tang05709/article/details/88366228
module Aliyunclass AliyunVideoAchieve < AliyunSigndef initialize@access_key_id = Rails.configuration.application['ALIYUN_OSS_ACCESS_KEY_ID']@access_key_secret = Rails.configuration.application['ALIYUN_OSS_ACCESS_KEY_SECRET']@send_url = 'http://vod.cn-shanghai.aliyuncs.com'.freezeend'''获取视频上传地址和凭证,并创建视频信息title: 视频标题file_name: 视频源文件名必须带扩展名options: FileSize: 视频文件大小; Description: 视频描述,长度不超过1024个字节, CoverURL:自定义视频封面URL地址options: CateId:视频分类ID;Tags:视频标签,多个用逗号分隔;TemplateGroupId:转码模板组ID; UserData:自定义设置,为JSON字符串,支持消息回调等设置options: StorageLocation:存储地址。当不为空时,会使用该指定的存储地址上传视频文件return RequestId:请求ID; VideoId:视频ID;UploadAddress:上传地址;UploadAuth:上传凭证''' def create_upload_video(title, file_name, options = {})#http://vod.cn-shanghai.aliyuncs.com/?Action=CreateUploadVideo&Title=exampleTitle&FileName=example.avi&FileSize=10485760&Format=JSON&<公共参数>#Action 系统规定参数。取值:CreateUploadVideo#Title视频标题,长度不超过128个字节 utf8#FileName视频源文件名必须带扩展名,且扩展名不区分大小写。MP4,3GP,MPEG,AVI,FLV,m3u8#FileSize 视频文件大小# 返回 RequestId:请求ID, VideoId:视频ID, UploadAddress:上传地址, UploadAuth:上传凭证param = {Action: 'CreateUploadVideo',Title: title,FileName: file_name,AccessKeyId: @access_key_id}key = @access_key_secret + '&'options = create_sign_url("POST", param, key, 'video')result = Faraday.post(@send_url, options)res = JSON.parse(result.body)if res["UploadAddress"].blank? []else resend end'''刷新视频上传凭证video_id 视频ID'''def refresh_upload_video(video_id)# http://vod.cn-shanghai.aliyuncs.com/?Action=RefreshUploadVideo&VideoId=93ab850b4f6f44eab54b6e91d24d81d4&Format=JSON&<公共参数># Action 系统规定参数。取值: RefreshUploadVideo# VideoId 视频ID# 返回 RequestId:请求ID, UploadAddress:上传地址, UploadAuth:上传凭证param = {Action: 'RefreshUploadVideo',VideoId: video_id,AccessKeyId: @access_key_id}key = @access_key_secret + '&'options = create_sign_url("POST", param, key, 'video')result = Faraday.post(@send_url, options)res = JSON.parse(result.body)if res["UploadAddress"].blank? []else resend end'''获取视频信息video_id 视频ID'''def get_video_info(video_id)#http://vod.cn-shanghai.aliyuncs.com/?Action=GetVideoInfo&VideoId=93ab850b4f6f44eab54b6e91d24d81d4&Format=JSON&<公共参数># Action 系统规定参数。取值:GetVideoInfo# VideoId 视频ID# 返回 RequestId, Video# Video: VideoId, Title, Description, Duration, CoverURL, Status, CreationTime, Snapshots, CateId, CateName, Tags# Snapshots: Snapshotparam = {Action: 'GetVideoInfo',VideoId: video_id,AccessKeyId: @access_key_id}key = @access_key_secret + '&'options = create_sign_url("POST", param, key, 'video')result = Faraday.post(@send_url, options)res = JSON.parse(result.body)if res["Video"].blank? []else resend end'''生成一个密钥'''def get_play_datakey # https://kms.cn-hangzhou.aliyuncs.com/?Action=GenerateDataKey# KeyId 主密钥(CMK)的全局唯一标识符# 返回 KeyId, Plaintext, CiphertextBlobsend_url = 'https://kms.cn-hangzhou.aliyuncs.com'param = {Action: 'GenerateDataKey',KeyId: "最好填写密钥别名",AccessKeyId: @access_key_id}key = @access_key_secret + '&'options = create_sign_url("POST", param, key, 'auth')result = Faraday.post(send_url, options)res = JSON.parse(result.body)puts resif res["CiphertextBlob"].blank? []else $redis.set('Plaintext', res["Plaintext"])$redis.set('CiphertextBlob', res["CiphertextBlob"])res["CiphertextBlob"]end end'''获取播放凭证video_id 视频IDtimeout 凭证过期时间, 取视频时长'''def get_play_auth(video_id, timeout = nil)# Action 系统规定参数。取值: GetVideoPlayAuth# VideoId 视频ID# AuthInfoTimeout 播放凭证过期时间。取值范围:100~3000。默认值:100秒# PlayConfig CipherText 密文秘钥,用来获取明文秘钥# PlayConfig DecryptKeyUri 根据密文秘钥获取解密秘钥地址# PlayConfig KeyServiceType 密钥服务类型,默认值:KMS# 返回 RequestId, VideoMeta, PlayAuth# VideoMeta VideoId, Title, Duration, CoverURL, Statusciphertext = $redis.get("CiphertextBlob")if ciphertext.blank? ciphertext = get_play_datakeyendplay_config = {CipherText: ciphertext,KeyServiceType: 'KMS'}play_config_str = play_config.to_jsonparam = {Action: 'GetVideoPlayAuth',VideoId: video_id,PlayConfig: play_config_str,AccessKeyId: @access_key_id}if timeout.present?param[:AuthInfoTimeout] = timeoutendkey = @access_key_secret + '&'options = create_sign_url("POST", param, key, 'video')result = Faraday.post(@send_url, options)res = JSON.parse(result.body)if res["PlayAuth"].blank? []else data = {PlayAuth: res["PlayAuth"],CoverURL: res["VideoMeta"]["CoverURL"],VideoId: res["VideoMeta"]["VideoId"]}end end'''删除视频video_ids 视频ID列表。多个用逗号分隔'''def delete_video(video_ids)# Action 系统规定参数。取值:DeleteVideo# VideoIds 视频ID列表。多个用逗号分隔,最多支持20个param = {Action: 'DeleteVideo',VideoIds: set_videos(video_ids),AccessKeyId: @access_key_id}key = @access_key_secret + '&'options = create_sign_url("POST", param, key, 'video')result = Faraday.post(@send_url, options)end'''phone_numbers, 逗号隔开'''def set_videos(video_ids)if video_ids.is_a(Array)videos = video_ids.join(',')else videos = video_idsendvideosendend
end
如果需要加密,需要在aliyun开启密钥服务,然后把密钥id写入代码,当如aliyun提供了密钥别名,所以最好用别名
前端
<div class="prism-player" id="J_prismPlayer"></div> <script> var player = new Aliplayer({ id: "J_prismPlayer", width:"1000px", height:"640px", format: 'm3u8',useFlashPrism: true,vid:"<%= @play_auth[:VideoId] %>", playauth:"<%= @play_auth[:PlayAuth] %>", cover:"<%= @play_auth[:CoverURL] %>" });</script>
当然,也需要引入js,因为就一个页面用,就直接在前端引用了
<link rel="stylesheet" href="//g.alicdn.com/de/prismplayer/2.6.0/skins/default/aliplayer-min.css" />
<script type="text/javascript" src="//g.alicdn.com/de/prismplayer/2.6.0/aliplayer-min.js"></script>
这篇关于ruby on rails aliyun视频点播播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!