阿里云视频点播服务添加视频字幕的实现方案

业务场景

点播服务上传的原始视频没有字幕,需要对点播视频加上视频字幕。

业务流程

  1. 上传字幕文件
  2. 添加带字幕的转码模版
  3. 使用带字幕的转码模版转码

使用步骤

  1. 上传字幕文件
    字幕文件无法通过控制台上传,需要通过上传SDK的上传接口上传,并且指定subtitle参数:
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadAttachedMediaRequest import UploadAttachedMediaRequest

def testUploadLocalAttachedMedia(accessKeyId, accessKeySecret, filePath):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadAttachedRequest = UploadAttachedMediaRequest(filePath, 'subtitle')
        uploadAttachedRequest.setTitle('test upload local watermark file')
        media = uploader.uploadAttachedMedia(uploadAttachedRequest, True)
        print(media)
    except AliyunVodException as e:
        print(e)

localFilePath = '/Users/mac/Downloads/VodUploadSDK-Python_1.3.1/dlfiles/subtitle-02.srt'
testUploadLocalAttachedMedia(accessKeyId, accessKeySecret, localFilePath)

上传完成后会返回字幕文件的url信息:
{'FileURL': u'https://outin-a30b2feff15a11e8ad9400163e1a625e.oss-cn-shanghai.aliyuncs.com/subtitle/5D55590D555C40BA8DC9BF307D3AC28C-3-3.srt', 'MediaId': u'68928b9ab5744b65b7b04f0cca97f54d', 'MediaURL': u'http://vod.pier39.cn/subtitle/5D55590D555C40BA8DC9BF307D3AC28C-3-3.srt'}

  1. 设置转码模版
    点播控制台的转码模版目前还不支持选择字幕,所以也是通过接口来完成对带字幕转码模版的设置的:
from aliyunsdkcore import client
from aliyunsdkvod.request.v20170321 import AddTranscodeTemplateGroupRequest

clt = client.AcsClient(access_key_id, access_key_secret, region)

def AddTranscodeTemplateGroup(clt):
    request = AddTranscodeTemplateGroupRequest.AddTranscodeTemplateGroupRequest()
    
    transcodeTemplateList = []
    transcodeTemplate = {}
    # 设置模板名称
    transcodeTemplate["TemplateName"] = "Subtitle_Transcode"
    # 清晰度
    transcodeTemplate["Definition"] = "LD"
    # 视频流转码配置
    videoConfig = {"Width": 640, "Bitrate": 400, "Fps": 25, "Remove": False, "Codec": "H.264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig
    # 音频流转码配置
    audioConfig = {"Codec": "AAC", "Bitrate": "64", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig
    # 封装容器
    container = {"Format": "m3u8"}
    transcodeTemplate["Container"] = container

    # 添加字幕参数
    subtitleList = {"SubtitleUrl":"https://outin-a30b2feff15a11e8ad9400163e1a625e.oss-cn-shanghai.aliyuncs.com/subtitle/D973C24DA668448A80E235F9D020BEFE-3-3.srt",
        "CharEncode":"UTF-8"}
    transcodeTemplate["SubtitleList"] = subtitleList
    request.set_TranscodeTemplateList(json.dumps(transcodeTemplateList))
    request.set_Name('subtitle')

    response = clt.do_action_with_exception(request)
    return response

print AddTranscodeTemplateGroup(clt)

代码里subtitleList参数是设置字幕的,里面的SubtitleUrl参数填写第一步中返回的FileURL地址

  1. 使用带字幕的转码模版转码
    这里可以通过控制台的媒体处理选项选择上面创建的转码模版,或者通过转码接口来实现转码:
from aliyunsdkcore import client
from aliyunsdkvod.request.v20170321 import SubmitTranscodeJobsRequest

clt = client.AcsClient(access_key_id, access_key_secret, region)

def SubmitTranscodeJobs(clt):
    request = SubmitTranscodeJobsRequest.SubmitTranscodeJobsRequest()
    request.set_VideoId('4bee0ee927ad4bed8ca865471c6680a7')
    request.set_TemplateGroupId('89a90b2cb8fdfdf9765747dd39fae31e')
    response = clt.do_action_with_exception(request)
    return response
 
print SubmitTranscodeJobs(clt)
  1. 播放通过上面转码模版转码后的视频就会有字幕的效果了

阿里云视频点播服务添加视频字幕的实现方案

上一篇:推荐40款强大的 jQuery 导航插件和教程【上篇】


下一篇:TensorFlow教程之进阶指南 3.6 线程和队列