简体   繁体   English

如何使用纯ecmascript从MP4 URL获取视频高度和宽度,并且没有h.264的浏览器支持?

[英]How to get video height and width from a MP4 URL with pure ecmascript and without browser support for h.264?

I'm writing a user script for downloading videos. 我正在编写用于下载视频的用户脚本。 The flash player of the web site use a JSON file. 该网站的Flash播放器使用JSON文件。
The purpose of my script is to get the url of the video by downloading and parsing the video according to the web page. 我的脚本的目的是通过根据网页下载和解析视频来获取视频的URL。 Currently it can download an extract the URL of the videos successfully. 目前,它可以成功下载提取视频的URL。

The important part of the JSON file look like this : JSON文件的重要部分如下所示:

    {
        "ammProfile": "AMM-HBBTV",
        "version": "VF",
        "versionProg": "1",
        "VFO": "HBBTV",
        "VMT": "mp4",
        "VUR": "http://vo.llnwd.net/v2/am/HBBTV/051332-074-A_SQ_2_VF_01464306_MP4-2200_AMM-HBBTV.mp4"
    }, {
        "ammProfile": "AMM-HBBTV",
        "version": "VF",
        "versionProg": "1",
        "VFO": "HBBTV",
        "VMT": "mp4",
        "VUR": "http://vo.llnwd.net/v2/am/HBBTV/051332-074-A_EQ_2_VF_01464315_MP4-1500_AMM-HBBTV.mp4"
    }

Both URL here are about the same video, this just is just the resolution which change. 这里的两个URL都是关于相同的视频,这只是改变的分辨率。

So, How I can parse the relevant metadata without downloading the whole file? 那么, 如何在不下载整个文件的情况下解析相关元数据呢? The standard for the H.264 video codec is very hard to read. H.264视频编解码器的标准很难阅读。

you don't need to load the whole video to get the height: 你不需要加载整个视频来获得高度:

function getVideoHeight(url, fnCallback){

var video=document.createElement("video");
  video.autoplay=true;
  video.oncanplay=function(){
     fnCallback(this.offsetWidth, this.offsetHeight);
     this.src="about:blank";
     document.body.removeChild(video);   
  };

  document.body.appendChild(video);
  video.src=url;

}


//test:

getVideoHeight(
  "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4", 
  function(w,h){ alert( w+"X"+h); }
); // shows: "640X360"

You can use the Range HTTP header via XMLHttpRequest to get only a portion of the file: 您可以通过XMLHttpRequest使用Range HTTP标头只获取文件的一部分:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

For example: 例如:

xhr.setRequestHeader ('Range', 'bytes=0-' + (fragment_size - 1))
xhr.setRequestHeader ('Content-Length', fragment_size) // This part isn't absolutely required on most (all?) browsers.

I use the xhr range header to download partial content, and then get the file info using videoconverter.js , a JS version of ffmpeg (whose license you should check if you plan to use any of this). 我使用xhr范围标题下载部分内容,然后使用videoconverter.js获取文件信息,fidempverte.js是ffmpeg的JS版本(如果您计划使用其中任何一个,则应检查其许可证)。

 var videoUrl = 'https://dl.dropboxusercontent.com/u/17698405/bla.mp4'; var cmd = '-i myfile.mp4'; var args = parseArguments(cmd); var sizeToDownload = 200*1024; retrieveVideo(videoUrl, sizeToDownload, function(fileData) { ffmpeg_run({ arguments: args, files: [{ name: 'myfile.mp4', data: fileData }], print: print, printErr: print }); }); function parseArguments(text) { text = text.replace(/\\s+/g, ' '); var args = []; text.split('"').forEach(function(t, i) { t = t.trim(); if ((i % 2) === 1) { args.push(t); } else { args = args.concat(t.split(" ")); } }); return args; } function retrieveVideo(path, fragment_size, callback) { var xhr = new XMLHttpRequest(); xhr.open("GET", path, true); xhr.responseType = "arraybuffer"; xhr.setRequestHeader ('Range', 'bytes=0-' + (fragment_size - 1)); xhr.onload = function (oEvent) { var arrayBuffer = xhr.response; if (arrayBuffer) { callback(new Uint8Array(arrayBuffer)); } }; xhr.send(null); } var textarea = document.getElementsByTagName('textarea')[0]; function print(text) { textarea.value += '> ' + text + '\\n'; } 
 * { box-sizing: border-box } html,body { height: 100%; margin: 0; padding: 0; overflow: hidden } textarea { width: 100%; height: 100%; } 
 <script src="https://rawgit.com/bgrins/videoconverter.js/master/build/ffmpeg-all-codecs.js"></script> <textarea></textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM