Youtube-API: Upload binary captions file (ebu-

2019-02-27 05:45发布

问题:

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*')

回答1:

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.