简体   繁体   English

上传前检查 MP4 是否为 H264 编码

[英]Checking if MP4 is H264 encoded before upload

I've built an app to manage uploading adverts, the marketing team choose whether they want an image or video advert and fill out a form with title, start time etc. This data is then stored in my db.我已经构建了一个应用程序来管理上传广告,营销团队选择他们想要图片还是视频广告,并填写带有标题、开始时间等的表单。然后这些数据存储在我的数据库中。

However, with videos there's an extra step, they're uploaded to AWS, and the way this is configured it will only work with H264 encoded videos.但是,对于视频,有一个额外的步骤,它们被上传到 AWS,并且这种配置方式仅适用于 H264 编码的视频。

I'd like to add some client side validation for videos, I found a related question which checks whether a browser supports H264:我想为视频添加一些客户端验证,我发现了一个相关的问题,它检查浏览器是否支持 H264:

How to detect supported video formats for the HTML5 video tag? 如何检测 HTML5 视频标签支持的视频格式?

This the code the answer uses to check support这是答案用于检查支持的代码

var testEl = document.createElement( "video" ),
    mpeg4, h264;

if ( testEl.canPlayType ) {
    // Check for h264 support
    h264 = "" !== ( testEl.canPlayType( 'video/mp4; codecs="avc1.42E01E"' )
        || testEl.canPlayType( 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"' ) );
   );

I've inspected the videos in the console, comparing one with H264 encoding to one without and I can't find anything within their attributes relating to H264.我检查了控制台中的视频,将带有 H264 编码的视频与不带有 H264 编码的视频进行了比较,但在与 H264 相关的属性中我找不到任何内容。 The src lists MP4 but nothing else: src 列出 MP4 但没有别的:

src: data:video/mp4;base648766554654 etc.

You need a MP4 parser for JavaScript, for example mp4box.js .您需要一个用于 JavaScript 的 MP4 解析器,例如mp4box.js

In the case of MP4Box you can append buffers until the moov box is parsed, which can happen at the end of the file in the worst case.在 MP4Box 的情况下,您可以附加缓冲区直到解析moov框,在最坏的情况下,这可能发生在文件末尾。

Example from the documentation:文档中的示例:

var MP4Box = require('mp4box').MP4Box;
var mp4box = new MP4Box();
mp4box.onError = function(e) {};
mp4box.onReady = function(info) {};
mp4box.appendBuffer(data);
mp4box.appendBuffer(data);
mp4box.appendBuffer(data);
...
mp4box.flush();

onReady() returns the file information: onReady()返回文件信息:

mp4box.onReady = function (info) {
    console.log("Received File Information");
}

In it you have tracks with the corresponding codec for each.在其中,您有带有相应codec tracks

For details on the codecs format see RFC 6381 .有关codecs格式的详细信息,请参阅RFC 6381

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

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