获得Youtube标题给定的id

Getting Youtube Title given an id

本文关键字:id Youtube 标题 获得      更新时间:2023-09-26

我找到了一个很好的参考,可以在给定youtube视频id的情况下获得youtube视频标题,但我不确定如何在他提供的函数中返回标题。在videoInfoCallback函数中,我看到它返回消息,但我不确定传递到info参数中的是什么。任何建议。这是我正在使用的资源:Parser

这是我正在玩的代码:

function registerScript(url) {
            var s = document.createElement('script');
            s.type = 'text/javascript';
            s.src = url;
            document.getElementsByTagName('head')[0].appendChild(s);
        }
        function videoInfoCallback(info) {
            if (info.error) {
                alert('Error'n'n' + info.error.message);
            } else {
                var message = info.data.title;
                return message;
            }
            return "hello";
        }

        function getVideoInformation(id) {
            if (id) {
                return registerScript('https://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=jsonc&callback=videoInfoCallback');
            } else {
                alert('Please enter an id.');
            }
        }

基本上,我想有一个函数,接收像CFF0mV24WCY这样的Youtube视频id,并让它返回标题。

它并不能真正回答您的上述问题,但它可能会帮助您完成任务

这只是一个简单的youtube搜索,它带来了标题视频和观看次数

html

<input type="text" id="Search"  /><br/>
<input id="show" type="button" value="Submit" onclick="Show();"/>
<div id="result">
</div>

JavaScript

$(document).ready(function () {
    $("#show").click(function () {
        getYoutube($("#Search").val() + "Offical Film Trailer");
    });
});
function getYoutube(title) {
    $.ajax({
        type: "GET",
        url: yt_url = 'http://gdata.youtube.com/feeds/api/videos?q=' + title + '&format=5&max-results=1&v=2&alt=jsonc',
        dataType: "jsonp",
        success: function (response) {
            if (response.data.items) {
                $.each(response.data.items, function (i, data) {
                    var video_id = data.id;
                    var video_title = data.title;
                    var video_viewCount = data.viewCount;
                    var video_frame = "<iframe width='600' height='385' src='http://www.youtube.com/embed/" + video_id + "' frameborder='0' type='text/html'></iframe>";
                    var final_res = "<div id='title'>" + video_title + "</div><div>" + video_frame + "</div><div id='count'>" + video_viewCount + " Views</div>";
                    $("#result").html(final_res);
                });

            } else {
                $("#result").html("<div id='no'>No Video</div>");
            }
        }
    });
}