jAckOdE
jAckOdE

Reputation: 2500

Is it possible to play an output video file from an encoder as it's being encoded?

I have a video file, and I need to encode it as H264/AVC and feed to client via HTTP. What i need is that i player at client side can play back the video as it is being encoded.

AFAIK, To enable player to play as the video is downloading, "moov atom" have to be placed at the begnning of the video file. However, encoders (ex: ffmpeg) always write "moov atom" at the end of file after it completes encoding.

Is there any way encoder can put "moov atom" at beginning of encode's output? Or play video without moov atom presence?

Thank in advances

LR

Upvotes: 10

Views: 11046

Answers (4)

Hut8
Hut8

Reputation: 6342

Perhaps it wasn't possible when @wombat57 wrote his answer, but it is possible now!

You want -movflags +frag_keyframe+empty_moov+faststart

As explained in this answer,

Normally, a MOV/MP4 file has all the metadata about all packets stored in one location (written at the end of the file, it can be moved to the start for better playback by adding faststart to the movflags, or using the qt-faststart tool). A fragmented file consists of a number of fragments, where packets and metadata about these packets are stored together. Writing a fragmented file has the advantage that the file is decodable even if the writing is interrupted (while a normal MOV/MP4 is undecodable if it is not properly finished), and it requires less memory when writing very long files (since writing normal MOV/MP4 files stores info about every single packet in memory until the file is closed). The downside is that it is less compatible with other applications.

Upvotes: 6

manast
manast

Reputation: 96

You can move the MOOV Atom to the beginning of a file by rewriting the file using a tool in ffmpeg called qt-faststart. You will need to compile it yourself from source code (but is quite easy at least in Linux / Mac OS). Just download the source of libavcodec, for example: http://libav.org/releases/libav-0.8.tar.xz

Untar it and go the tools directory, there is a file called qt-faststart.c, just build it with:

make qt-faststart

you can now reallocate MOOV Atom by calling it like this: qt-faststart input.mp4 output.mp4

Upvotes: 2

Muhammad Razib
Muhammad Razib

Reputation: 1305

I also tried this same thing earlier with ffmpeg, but could not succeed. But with vlc I was able to download, transcode and stream on Android simultaneously, though with rtsp. I did not try with http progressive method. I used mpeg4 video codec, mpega audio codec in vlc.

Upvotes: 1

wombat57
wombat57

Reputation: 1341

Yes, this is possible, but only in some container formats. It is NOT possible with a QuickTime/MP4 container. In these formats, the moov atom contains sample offsets (the locations of the samples in the mdat atom). These are not known until after the video has been encoded. With VOD (video on demand) you can take the finished file, and move the moov atom to the front, to make streaming work better. But there is no way to do this if you are encoding on the fly. To make that work, you'll need to use a stream-oriented transport format. Something like FLV or MPEG-TS would work. If you pass video into ffmpeg and tell it to produce H.264 video in an FLV container, you can then serve that data to a player as it's encoded, and it will work. Of course, if you want to serve it over HTTP, you'll probably have to write your own server (or module for an existing server). Nothing that I know of supports serving a file as it is written (an issue is that the file size is not known when the content-length header is sent). If you serve the video over RTMP or RTSP, however, you can make this work with existing software.

Upvotes: 15

Related Questions