【发布时间】:2013-08-14 01:44:59
【问题描述】:
我需要在我的 java webapp 中阅读来自第三方、公开可用的 YouTube 视频的隐藏式字幕文本,即我尚未上传内容。
虽然v2 of the YouTube Data API 限制上传视频的人访问字幕信息,但允许访问除这一数据之外的所有内容似乎是一个非常奇怪的限制。我预计会在v3 of the API 中看到此限制被删除,但现在对隐藏式字幕的唯一引用是一种布尔方法,用于确认 CC 是否附加到视频。即使是所有者现在似乎也无法下载它。 (Google 至少会重新添加它吗?)
Boolean hasCaptions = video.getContentDetails().getCaption()
使用 YouTube Data API v3(使用 Google Java API client)我已经能够找到、验证和检索 YouTube 资源(视频、播放列表、频道等)。我几乎可以完成 API 提供的所有操作,只是无法阅读实际的标题文本。
我也尝试过unpublished timed text link workaround,但这不一致,不适用于较新的内容,并且它所涵盖的内容中有许多编码错误。
我想知道是否有人知道从 Java(不是 .js 插件)的 YouTube 视频中检索字幕文本的方法?
[ 最糟糕的情况,有没有人知道一个库,它允许我像浏览器一样以编程方式与 YouTube 交互,并且允许我单击屏幕上的脚本按钮,我可以从那里提取脚本? Prowser 不允许点击交互,JxBrowser 是 $1,300+]
下面的代码运行良好,可以让我访问所有视频数据,因此这是我需要帮助的最后一步。我已将其包含在此处,以防它对需要达到此目标的任何人有所帮助。
// Build a YouTube resource
YouTube youtube = new YouTube.Builder(new NetHttpTransport(),
new JacksonFactory(),
new HttpRequestInitializer())
.setApplicationName("caption-retrieval")
.build();
// Create the video list request, it should only return one
// result
YouTube.Videos.List listVideosRequest = youtube.videos().list("id, snippet, contentDetails");
listVideosRequest.setKey(API_KEY));
listVideosRequest.setId(VIDEO_ID);
// Request is executed and video list response is returned
VideoListResponse listVideosResponse = listVideosRequest.execute();
List<Video> videos = listVideosResponse.getItems();
// Since a unique video id is given, it will only return
// one video. Would check if video has been removed in
// production code.
Video video = videos.get(0);
// Read the remaining meta information
title = video.getSnippet().getTitle().trim();
author = video.getSnippet().getChannelTitle();
captionText = ???????
感谢您的帮助。
谢谢,
格雷格。
【问题讨论】:
标签: java youtube-api google-api closed-captions