Youtube-API:上传二进制字幕文件 (ebu-stl) [英] Youtube-API: Upload binary captions file (ebu-stl)

查看:30
本文介绍了Youtube-API:上传二进制字幕文件 (ebu-stl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Youtube 支持一些二进制字幕文件格式,,它们是字节相同的.

对我来说,这似乎是一个编码问题 - youtube 正确接收文件,但可能会将整个二进制文件解析为 UTF-8.但是,当我省略 charset=Content-Language 时,youtube 会返回 4xx 错误.

是否可以上传二进制字幕文件?如果不可能,我也将不胜感激.

简约的 stl 文件,如果您想自己尝试(十六进制转储). 将其读取为 (ruby):

stl = hex.chars.each_slice(2).map{|x|x.join.to_i(16)}.pack('C*')

解决方案

API v2 现已正式弃用.自 4 月 2 日起,API v3 现在正式支持字幕.也支持二进制字幕文件.

文档缺少有关原始 HTTP 请求的一些详细信息.您可以尝试使用他们的一个库.

我使用了他们的 python 脚本并查看了它发出的请求.作为参考,这是上传二进制字幕/字幕文件的最小工作 HTTP 请求.

docs 没有提到这个参数,但是设置了 uploadType=multipart 很重要,否则 API 会返回错误.

POST/upload/youtube/v3/captions?uploadType=multipart&part=id,snippet&sync=false HTTP/1.1主机:www.googleapis.com接受: */*授权:承载 {AUTH_TOKEN}X-GData-Key:key={YOUR_KEY}内容类型:多部分/相关;边界================83250640405719953005==内容长度:7147期望:100-继续--===============83250640405719953005==内容类型:应用程序/jsonMIME 版本:1.0{"snippet":{"videoId":"Agn_uesF248","language":"en","name":"Subtitle Test 2","isDraft":false}}--===============83250640405719953005==MIME 版本:1.0内容类型:应用程序/SLA内容传输编码:二进制{BINARY_CAPTION_FILE_DATA}--===============83250640405719953005==--

application/sla 是 ebu-stl 字幕文件.将 text/plain 用于纯文本字幕(.srt、.vtt、.ass、&c.).

{AUTH_TOKEN}{YOUR_KEY} 替换为 适用于 https://www.googleapis.com/auth/youtube.force-ssl 范围的授权凭据;和 {BINARY_CAPTION_FILE_DATA} 以及您要上传的文件.

Youtube supports some binary caption file formats, such as ebu-stl.

I've got an *.stl file that uploads and processes just fine when I upload it via the web interface. But when I try to upload it via the API v2 with a POST request, it does not seem to recognize the file format properly. The POST request looks like this:

POST /feeds/api/videos/VIDEO_ID/captions HTTP/1.1
Host: gdata.youtube.com
Content-Type: application/vnd.youtube.timedtext; charset=UTF-8
Content-Language: en
Slug: Title of caption track
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY

<Caption File Data>

This is how the *.stl file uploaded via the API looks on the website. There should be several lines with readable text.

Everything works fine when I upload a utf8 plain text subtitle file (eg *.vtt) with the same code. Furthermore, if I upload the stl file once via the web interface and once via the API, and retrieve both files through the API afterwards, they are byte-identical.

It looks like an encoding issue to me- youtube receives the file correctly, but probably parses the entire binary file as UTF-8. However, youtube responds with a 4xx error when I ommit either charset= or Content-Language.

Is it possible to upload binary caption files? I would also appreciate confirmation in case it is not possible.

Minimalistic stl file if you want to try it yourself (hex dump). Read it as (ruby):

stl = hex.chars.each_slice(2).map{|x|x.join.to_i(16)}.pack('C*')

解决方案

API v2 is now officially deprecated. As of April 2, the API v3 now officially support captions. Binary caption files are supported, too.

The documentation is lacking some details as to the raw HTTP requests. You could try using one of their libraries.

I used their python script and took a look at the request it made. For reference, here's a minimal working HTTP request for uploading a binary subtitle/captions file.

The docs aren't mentioning this parameter, but setting uploadType=multipart is important, the API returns an error otherwise.

POST /upload/youtube/v3/captions?uploadType=multipart&part=id,snippet&sync=false HTTP/1.1
Host: www.googleapis.com
Accept: */*
Authorization: Bearer {AUTH_TOKEN}
X-GData-Key: key={YOUR_KEY}
Content-Type: multipart/related; boundary================83250640405719953005==
Content-Length: 7147
Expect: 100-continue

--===============83250640405719953005==
Content-Type: application/json
MIME-Version: 1.0

{"snippet":{"videoId":"Agn_uesF248","language":"en","name":"Subtitle Test 2","isDraft":false}}
--===============83250640405719953005==
MIME-Version: 1.0
Content-Type: application/sla
Content-Transfer-Encoding: binary

{BINARY_CAPTION_FILE_DATA}
--===============83250640405719953005==--

application/sla are ebu-stl caption files. Use text/plain for plain text subtitles (.srt, .vtt, .ass, &c.).

Replace {AUTH_TOKEN} and {YOUR_KEY} with the proper authorization credentials for the scope https://www.googleapis.com/auth/youtube.force-ssl; and {BINARY_CAPTION_FILE_DATA} with the file you wish to upload.

这篇关于Youtube-API:上传二进制字幕文件 (ebu-stl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆