是否可以从单个流字节范围块创建新的mp4文件? [英] Is it possible to create new mp4 file from a single streaming byte range chunk?

查看:164
本文介绍了是否可以从单个流字节范围块创建新的mp4文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在支持字节范围的服务器上有一个远程mp4文件,是否可以检索单个字节范围,并从该范围数据创建一个新的/独立的mp4?

If I have a remote mp4 file on a server that supports Byte Ranges, is it possible to retrieve a single byte range and create a new/self-contained mp4 from that range data?

如果我使用 fs.createWriteStream(remoteFilename)直接将返回的字节范围数据直接写入mp4文件,则不会获得视频元数据(持续时间,维度等),它需要播放。

If I try and write a returned byte range data directly to an mp4 file using fs.createWriteStream(remoteFilename) it doesn't get the video meta data (duration, dimensions, etc) that it needs to be playable.

当我获得以0开头并以XX结尾的字节范围时,输出mp4是可播放的,但将具有整个视频长度的持续时间元数据,并将当字节范围在持续时间的剩余时间内完成时,冻结屏幕。

When I get a byte range that starts with 0 and ends with XX the output mp4 is playable, but will have the duration meta-data of the entire video length and will freeze the screen when the byte range is done for the remainder of the duration time.

我还可以采取一个字节范围并创建一个独立的.mp4文件那个流对象?

How else can I take a byte range and create a stand-alone .mp4 file from that stream object?

这一切的全部要点是避免下载整个10分钟的文件,然后才能使用ffmpeg进行5秒的剪辑。如果我可以计算和下载字节范围,应该有一种方式将其写入独立的mp4文件。

The whole point of this is to avoid downloading the entire 10 minute file before I can make a 5 second clip using ffmpeg. If I can calculate and download the byte range, there should be a way to write it to a standalone mp4 file.

提前感谢您提供的任何帮助。

Thanks in advance for any help you can provide.

推荐答案

MP4文件的框架结构。他们中的两个主要是moov和mdat(一般的非碎片MP4):

MP4 files are structured with boxes. Two main of them being moov and mdat (general case of non-fragmented MP4):


  • moov框:包含其他框:) - 每个包含有关mdat框中存在的编码数据的信息(moov =关于MP4文件的元数据)。典型的metadatas是持续时间,帧率,编解码器信息,视频/音频帧的参考...

  • mdat box:包含文件的实际编码数据。它可以来自各种编解码器,并且包括音频和视频数据(或者只有其中一个如果恰好是)。对于H264,NAL单元包含在mdat框中。

MP4的文件开头是(应该是)文件网络传递,所以如果你写一个从0到XX的字节范围请求,你可能会得到整个moov框+一定量的mdat数据。因此,文件可以播放到某一点。如果你的字节范围从YY到XX机会,你不会得到一个体面的moov框,但是很多mdat这样不能使用,除非它们重新打包在一个MP4文件中,使用适当的moov框引用关于切mdat的信息

The moov box is (should be) at the beginning of the file for MP4 file web delivery so if you write a byte range request from 0 to XX you will likely get the whole moov box + a certain amount of mdat data. Hence the file can be played up to a certain point. If you byte range from YY to XX chances are you will not get a decent moov box but a lot of mdat which as such cannot be used unless they are repack in a MP4 file with a proper moov box referencing information about the "cut" mdat.

可以从字节范围块重新创建一个有效的MP4文件,但需要对MP4文件格式结构的高级知识(您需要检索moov框以及使其可承受)。 MP4文件格式基于ISO / IEC 14496规定的 ISO基础媒体文件格式 -12(MPEG-4 Part 12)。

It is possible to recreate a valid MP4 file from a byte range chunk but it requires an advanced knowledge of the MP4 file format structure (you need to retrieve the moov box as well to make it bearable). MP4 file format is based on ISO base media file format - that was specified as ISO/IEC 14496-12 (MPEG-4 Part 12).

我知道2个可以帮你做所需的lib:一个在 PHP Java 。我不知道这样一个lib存在于node.js(我猜想它可以被移植)。即使您不使用它们,上面的2个库包含有关该主题的有价值的信息。

I know 2 libs that could help doing what you want: one in PHP and one in Java. I do not know if such a lib exists for node.js (I guess it could be ported). Even if you do not use them the 2 libs above contain valuable information about the subject.

为了提供您的问题的答案,您可以用不同的角度解决问题。了解您所需的文件中的哪一部分,您可以执行一个ffmpeg命令将全长MP4文件服务器端拼接成一个更小的文件,然后使用这个新的更小的MP4文件(因此您不需要在客户端下载不必要的数据)。

To provide an answer to your question you could tackle the issue with a different angle. Knowing which part of the file you want in milliseconds you could execute an ffmpeg command to splice the full-length MP4 file server-side into a smaller one and then do what you need with this new smaller MP4 file (as so you do not need to download unnecessary data on the client).

ffmpeg命令(在这种情况下,从文件开头1分钟):

ffmpeg command for that is (in this case cut at 1 minute from beginning of file):

ffmpeg -i input.mp4 -ss 00:00:00.000 -t 00:01:00.000 -c:a copy -c:v copy output.mp4

有关以上命令行

这样做非常快, MP4文件结构只是重新组织而无需重新转码。

This is done pretty fast as the MP4 file structure is just re-organised with no re-transcoding.

编辑: 或者我可以在遥控器上使用ffmpeg文件并在本地创建新剪辑

ffmpeg -ss 00:01:00.000 -i "http://myfile.mp4" -t 00:02:00.000 -c:a copy -c:v copy output.mp4

假设您在客户端(app / web)上有ffmpeg,如果您运行上述命令,ffmpeg将把mp4提取到输入的URL,然后寻找1分钟,并从那里剪下2分钟,然后写入生成内容到output.mp4 本地(当然没有下载完整的文件)。

Assuming you have ffmpeg on your client (app/web) if you run the above command ffmpeg will fetch the mp4 to the input URL then seek to 1 min and cut 2 min from there and so write the generated content to output.mp4 locally (without downloading the full file of course).

ffmpeg需要支持 http协议输入(您将在大多数二进制文件中找到)。您可以阅读这里关于在哪里放置-ss参数(优点/缺点)的更多信息。

ffmpeg needs to be built with support for http protocol input (which you will find in most binaries). You can read here for further information on where to place the -ss parameter (pros/cons).

这篇关于是否可以从单个流字节范围块创建新的mp4文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆