0

Let's say I start an FFmpeg job that takes hours and hours:

$ ffmpeg -i large.mp4 out.mp4

Is there any way I can preview the finished part of out.mp4 while the rest is yet to be encoded?

Trying this complains "moov atom not found". If I am to speculate why, it would be because playback requires updated and correct header information to be present at the beginning of the file, and FFmpeg doesn't bother to do this while encoding files; it waits until it's completely finished.

forthrin
  • 1,907
  • 6
  • 23
  • 31
  • @dsstorefile1: The output container is largely preferred to be MP4, since this allows for instant compatibility with portable Apple devices, plus Quicklook previewing in MacOS. I assume you are hinting to MKV, which I tried now and can confirm lets itself be instantly previewed during conversion (in VLC). But the question stands for MP4. – forthrin Jan 13 '19 at 13:16
  • You could take a look at the [tee muxer](https://superuser.com/a/1391996/110524). – llogan Jan 14 '19 at 19:12
  • @llogan: Very nice tip! Thanks! However, this forces you to play the movie throughout the entire conversion; You can't "jump in and out" of viewing it at your own prerogative. – forthrin Jan 14 '19 at 20:17

1 Answers1

1

Simply put, no. MP4 files cannot be viewed unless they're done encoding. As you correctly identified, in order to start playback, a player needs to parse the moov atom, which contains important info about what's to be played. For a description, you can have a look at the QuickTime File Format specification, which became the basis for MPEG-4 Part 14, or short, the MP4 format.

This atom can only be written once the media tracks are completely encoded. It's not that ffmpeg “doesn't bother" to write it—you just have to wait until all data is there to have all the information that is required.

The atom then gets written at the end of the container. For faster playback, you can move this atom to the beginning of the file (e.g., using qt-faststart or with the ffmpeg option -movflags +faststart). This, of course, can also only be done once encoding has finished.

That said, fragmented MP4s can be previewed up until a certain point, as Gyan suggests, by adding the following option:

-movflags +frag_keyframe+separate_moof+omit_tfhd_offset+empty_moov

What you could also do is switch over to a segmented encoding method, e.g. using the segment muxer. These create partial clips up to a predefined length, which means that you can start previewing the existing segments. You could then later join the segments back into one file with stream-copying.

slhck
  • 223,558
  • 70
  • 607
  • 592
  • Possible-ish: https://video.stackexchange.com/q/18177/1871 – Gyan Jan 13 '19 at 14:16
  • 1
    Good and well-written answer. PS! If you know the inner workings of the formats: Since incomplete MKV files play out of the box while MP4 files must be completed along with this "atom" thing, could it be considered an unnecessary and bad design decision in MP4? PPS! I would say the question is a duplicate of the indicated link, so feel free to mark it as such. – forthrin Jan 13 '19 at 16:38
  • 1
    @forthrin Yes, it's not a very robust design. In fact, many times files get corrupted during recording if, for example, the camera shuts down before being able to write the moov atoms. Duplicates can only be marked on the same site, so there's not much we can do, but it's fine if the question stays here. – slhck Jan 13 '19 at 18:45