JavaScript、php 获得 YouTube 视频缩略图和标题

<script>
function getScreen( url, size )
{
  if(url === null){ return ""; }

  size = (size === null) ? "big" : size;
  var vid;
  var results;

  results = url.match("[\\?&]v=([^&#]*)");

  vid = ( results === null ) ? url : results[1];

  if(size == "small"){
    return "http://img.youtube.com/vi/"+vid+"/2.jpg";
  }else {
    return "http://img.youtube.com/vi/"+vid+"/0.jpg";
  }
}

imgUrl_big     = getScreen("http://www.youtube.com/watch?v=9lp0IWv8QZY&feature=featured"); 
imgUrl_big2   = getScreen("uVLQhRiEXZs"); 
imgUrl_small  = getScreen("uVLQhRiEXZs", 'small');

document.write('<img src="' + imgUrl_big + '" /><br><br>');
document.write('<img src="' + imgUrl_big2 + '" /><br><br>');
document.write('<img src="' + imgUrl_small + '" />');
</script>

 

You can pass a YouTube video URL or video id and the function will return a path to the video image. The second function argument is optional.

 

You can specify the size of returned image.

It can be big (320x240) or small (128x96), defaults to big .

 

 

php 版

 

 

<?php 
/** 
 *  parse_youtube_url() PHP function 
 *  Author: takien 
 *  URL: http://takien.com 
 *  
 *  @param  string  $url    URL to be parsed, eg:  
 *                            http://youtu.be/zc0s358b3Ys,  
 *                            http://www.youtube.com/embed/zc0s358b3Ys
 *                            http://www.youtube.com/watch?v=zc0s358b3Ys 
 *  @param  string  $return what to return 
 *                            - embed, return embed code 
 *                            - thumb, return URL to thumbnail image
 *                            - hqthumb, return URL to high quality thumbnail image.
 *  @param  string     $width  width of embeded video, default 560
 *  @param  string  $height height of embeded video, default 349
 *  @param  string  $rel    whether embeded video to show related video after play or not.

 */  
  
function parse_youtube_url($url,$return='embed',$width='',$height='',$rel=0)
{ 
    $urls = parse_url($url); 
     
    //expect url is http://youtu.be/abcd, where abcd is video iD
    if($urls['host'] == 'youtu.be') $id = ltrim($urls['path'],'/'); 
    //expect  url is http://www.youtube.com/embed/abcd 
    else if(strpos($urls['path'],'embed') == 1) $id = end(explode('/',$urls['path'])); 
     //expect url is abcd only 
    else if(strpos($url,'/')===false) $id = $url; 
    //expect url is http://www.youtube.com/watch?v=abcd 
    else{ 
        parse_str($urls['query']); 
        $id = $v; 
    } 
    //return embed iframe 
    if($return == 'embed') return '<iframe width="'.($width?$width:560).'" height="'.($height?$height:349).'" src="http://www.youtube.com/embed/'.$id.'?rel='.$rel.'" frameborder="0" allowfullscreen>'; 
    //return normal thumb 
    else if($return == 'thumb') return 'http://i1.ytimg.com/vi/'.$id.'/default.jpg'; 
    //return hqthumb 
    else if($return == 'hqthumb') return 'http://i1.ytimg.com/vi/'.$id.'/hqdefault.jpg';
    // else return id 
    else return $id; 
} 

// example
echo '<img src="'.parse_youtube_url('http://www.youtube.com/watch?v=QM-CvD8GQS4&feature=player_embedded','hqthumb').'" />'; //return http://i1.ytimg.com/vi/zc0s358b3Ys/hqdefault.jpg
echo parse_youtube_url('http://www.youtube.com/watch?v=QM-CvD8GQS4&feature=player_embedded','embed'); //return embed code (iframe) 
?>

 

 

 

以上php可以获取id值,利用id值就可以获取他的图片或者标题,


获取标题:


 

<?
    $vidID = $_POST['vidID'];
    $url = "http://gdata.youtube.com/feeds/api/videos/".$vidID;
    $doc = new DOMDocument;
    $doc->load($url);
    $title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
?>

<html>
    <head>
        <title>Get Video Name</title>
    </head>
    <body>
        <form method="post">
            <input type="text" value="ID Here" name="vidID" />
            <input type="submit" value="Get Name" />
        </form>
        <div id="page">URL: [<?= $url ?>]</div>
        <div id="title">Title: [<?= $title ?>]</div>
    </body>
</html>

 

 

或者:

 

<?php
$video_id = 'BGCqmjxQGOE';
$content = file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id);
parse_str($content, $ytarr);
echo $ytarr['title'];
?>

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 HTML 和 JavaScript 中生成 Word 文档缩略图,可以使用 Office.js 库提供的 API,以下是一个简单的示例代码: ```html <!-- 引入 Office.js 库 --> <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script> <!-- 创建 Word 文档容器 --> <div id="document-container" style="display:none"></div> <!-- 创建缩略图容器 --> <div id="thumbnail-container"></div> <script> // 初始化 Office.js 库 Office.initialize = function () { // 加载 Word 文档 Word.run(function (context) { var body = context.document.body; // 将 Word 文档插入到容器中 body.getHtmlAsync(function (result) { var html = result.value; document.getElementById('document-container').innerHTML = html; // 将 Word 文档转换为缩略图 var image = new Image(); image.src = 'data:image/svg+xml,' + encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml">' + document.getElementById('document-container').innerHTML + '</div></foreignObject></svg>'); document.getElementById('thumbnail-container').appendChild(image); }); return context.sync(); }); }; </script> ``` 在上面的代码中,我们首先引入了 Office.js 库,并创建了一个隐藏的容器用于加载 Word 文档。然后,我们使用 Office.js 的 API 将 Word 文档插入到容器中,并将其转换为缩略图并显示在页面上。 请注意,此示例代码需要在支持 Office.js 的环境中运行,例如 Microsoft Office Online 或 Office 客户端应用程序。对于其他环境,可能需要使用其他工具或库来生成 Word 文档缩略图

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值