我可以使用 FFmpeg 转码文件并动态读取输出文件吗?

我可以使用 FFmpeg 转码文件并动态读取输出文件吗?

我有一个 .M3U8 流,我可以下载它并用 FFmpeg 转换它: ffmpeg -protocol_whitelist file,http,https,tcp,tls -i "org_file.m3u8" -c copy -bsf:a aac_adtstoasc output.mp4 但是在转码时我无法打开 output.mp4 文件。

答案1

常规 MP4 文件在转码时无法播放,因为一旦编码完成,MOOV 原子(解析文件所需的)就会写入末尾。在许多情况下,它然后移动到文件的开头 – 这就是所做-movflags +faststart的 – 但您仍然必须等待文件被写入一次。

碎片 MP4 (fMP4) 文件是一种特殊的 MP4 文件,可以在写入的同时进行读取,请参阅这个答案.使用-movflags +frag_keyframe+separate_moof+omit_tfhd_offset+empty_moov以启用碎片整理。

只要写入了足够多的数据,MPEG-2 传输流文件就可以在写入的同时播放。您可以尝试将数据写入文件.ts,然后等待几秒钟,然后开始播放。

如果您想实时写入数据,您必须添加全局选项-re,这使得 ffmpeg 实时读取输入,因此写入速度不会比您播放的速度更快。

答案2

使用 ISMV 格式(-f ismv)似乎会自动选择正确的-movflags选项,以便能够在编码时读取输出文件:

$ ffmpeg .... -f ismv output.mp4
$ man ffmpeg-formats | grep ismv -B5
           Set the sample rate for libopenmpt to output.  Range is from 1000 to INT_MAX. The value default is 48000.

   mov/mp4/3gp
       Demuxer for Quicktime File Format & ISO/IEC Base Media File Format (ISO/IEC 14496-12 or MPEG-4 Part 12, ISO/IEC 15444-12 or JPEG 2000 Part 12).

       Registered extensions: mov, mp4, m4a, 3gp, 3g2, mj2, psp, m4b, ism, ismv, isma, f4v
--

               ffmpeg -i INPUT -f md5 -

       See also the hash and framemd5 muxers.

   mov, mp4, ismv
       MOV/MP4/ISMV (Smooth Streaming) muxer.

       The mov/mp4/ismv muxer supports fragmentation. Normally, a MOV/MP4 file has all the metadata about all packets stored in one location (written at
--
       -movflags empty_moov
           Write an initial moov atom directly at the start of the file, without describing any samples in it. Generally, an mdat/moov pair is written at
           the start of the file, as a normal MOV/MP4 file, containing only a short portion of the file. With this option set, there is no initial mdat
           atom, and the moov atom only describes the tracks but has a zero duration.

           This option is implicitly set when writing ismv (Smooth Streaming) files.

       -movflags separate_moof
           Write a separate moof (movie fragment) atom for each track. Normally, packets for all tracks are written in a moof atom (which is slightly more
           efficient), but with this option set, the muxer writes one moof/mdat pair for each track, making it easier to separate tracks.

           This option is implicitly set when writing ismv (Smooth Streaming) files.
--
       -movflags negative_cts_offsets
           Enables utilization of version 1 of the CTTS box, in which the CTS offsets can be negative. This enables the initial sample to have DTS/CTS of
           zero, and reduces the need for edit lists for some cases such as video tracks with B-frames. Additionally, eases conformance with the DASH-IF
           interoperability guidelines.

           This option is implicitly set when writing ismv (Smooth Streaming) files.
--

       Example

       Smooth Streaming content can be pushed in real time to a publishing point on IIS with this muxer. Example:

相关内容