53

I am working on live device to server streaming in android. I am able to send data in bytes on server but when I play that file during recording on server VLC say that MOOV atom not found. After a lot of workaround I found that MOOV atom of a mp4 file generates in the end. But I have to play that file on server while recording means live. I go through the source code of SPYDROID and SIPDROID but non of them is working. I tried to add moov atom on serverside using FFMPEG but didn't get any success. Anyone has an idea on how can I achieve this?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Ravinder
  • 539
  • 1
  • 4
  • 4
  • Possible duplicate of [Post processing in ffmpeg to move 'moov atom' in MP4 files (qt-faststart)](https://stackoverflow.com/questions/8061798/post-processing-in-ffmpeg-to-move-moov-atom-in-mp4-files-qt-faststart) – llogan Oct 08 '18 at 18:19
  • Can I use ffmpeg to resolve a problem I am having playing TV series on Plex. The logs indicate: – markum Dec 29 '20 at 08:32
  • For more clarification i am running my plex and nas servers on raspberry pi 4. I am using 'MacX DVD Ripper Pro' to rip the videos. I moved to the newest version of plex (1.21.1.3830) which should allow a new scanner – markum Dec 29 '20 at 08:51

7 Answers7

35

There is a tool untrunc which claims to repair unfinished (truncated) mp4, m4v, mov, 3gp video. I haven't tested it myself but it may be worth a try.

k3a
  • 1,296
  • 1
  • 15
  • 32
  • 2
    For Windows use: https://www.videohelp.com/software/recover-mp4-to-h264 – user136036 Feb 27 '20 at 17:36
  • 32
    This often did not work for my videos, but I had success with [Anthon Lockwood's fork](https://github.com/anthwlock/untrunc): It contains a "-s" switch to "step through unknown sequences" – Johannes Mar 23 '20 at 21:45
  • 3
    Tested other softwares, none have repaired my broken file, but only this. Congratulations for its developer! And @k3a: thanks for the tip. – Zsolti Jun 19 '20 at 20:16
  • 1
    there is even an online service for it http://untrunc.it/ – Roman May 05 '21 at 16:44
  • 1
    There is a [fork](https://github.com/anthwlock/untrunc) that you can install from Snap if you don't want to go through the hussle of building it from source – oeter Jan 29 '23 at 07:59
22

You got a problem. The 'moov' box is a kind of table of contents. If not all content is there you can't have a complete table of contents. Ouch!

If you want to stick with MP4 and if you are writing the file by yourself you could write the file as so called fragmented MP4 file. A fragmented MP4 file contains multiple self-contained small pieces of the video - each with its own table of contents. It would enable you to play the file before the complete recording has finished.

If you don't need to stick with MP4 an option would be to write the raw h264 stream to the server. Then you don't have that kind of table of content. VLC can play raw h264 streams.

Sebastian Annies
  • 2,438
  • 1
  • 20
  • 38
14

It is possible to move the moov atom to the begining of the video file using FFMpeg.

ffmpeg -i input_video_file.mp4 -vcodec copy -acodec copy -movflags faststart output_video_file.mp4
GG.
  • 21,083
  • 14
  • 84
  • 130
Abdullah Farweez
  • 851
  • 2
  • 11
  • 25
  • here is a quick reference guide: https://gist.github.com/jaydenseric/220c785d6289bcfd7366 – Abdullah Farweez May 05 '18 at 07:58
  • 3
    there is a typo here, "+faststart" should be "faststart", correct command is "ffmpeg -i input_video_file.mp4 -vcodec copy -acodec copy -movflags faststart output_video_file.mp4" – Joey May 3 '18 at 7:44 – Xueshi Feb 12 '19 at 08:22
  • 28
    @RoubenTchakhmakhtchian Despite the OP's title, this is not a technique to add a missing moov atom, just one to move an existing moov from the end to the beginning of a file (which is effectively what was actually asked for in the question). If you don't have a moov at all, you have a bigger problem. – Bob Sammers Sep 09 '19 at 08:30
  • Thanks, @BobSammers! – Rouben Tchakhmakhtchian Sep 12 '19 at 18:05
  • 1
    Worked perfectly for me! – DanB Oct 04 '19 at 15:02
  • I renamed the video file to `input_video_file.mp4` and copy&paste the exact command. However, ffmpeg still yields the following errors: `[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55601375c0c0] moov atom not found` and `input_video_file.mp4: Invalid data found when processing input ` – TJM Dec 13 '21 at 10:47
4

Run qt_faststart to move the moov atom to the beginning of the stream.

qt-faststart in.mp4 out.mp4
d33pika
  • 1,997
  • 14
  • 24
  • 3
    hi d33pika..thnx for ur rply, i want to send video data on server while recording, but a moov atom generates after recording finished so thats why i think qt-faststart will not be helpful here. – Ravinder Jul 19 '12 at 07:12
  • Have you solved your problem @Ravinder ? I am trying to do exactly the same right now and don't see a viable solution. If you know one, please post a comment here. – Oliver Hausler Nov 16 '14 at 18:33
  • How do you use qt-faststart in android? Can some one post an example code? – Kannan_SJD Jun 23 '16 at 04:49
  • 3
    I tried this but it says "last atom in file was not a moov atom" – KansaiRobot Apr 13 '22 at 01:06
  • 1
    `encountered non-QT top-level atom (is this a QuickTime file?) [...]` – silverdr Aug 19 '22 at 15:23
2

the mp4 format needs the moov atom information to play the video, and to generate the moov atom the video must be finished, you can't play a mp4 file while it is recording because you still don't have all the information to create the moov atom part.

What you want to do is some kind of real-time-streaming (play while is recroding) so you need to use another format. HLS streaming and mpeg-dash stores the video in tiny chunks (2 seconds to 10 seconds) and send to the users, this way the users plays many finished files one after the other.

as @Sebastian Annies suggested, to create many tiny mp4 files and concatenate is the same approach: to have tiny finished files and play as a list, here you could get more information What exactly is Fragmented mp4(fMP4)? How is it different from normal mp4?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Cristian Sepulveda
  • 1,572
  • 1
  • 18
  • 25
1

Use MP4Box to move MOOV atom at begening of file and interleaving to stream in chunk.

MP4Box test.mp4 test.mp4
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
Hemant
  • 181
  • 3
  • 14
  • 17
    It doesn't work for me. It returns: Error - 2 input names specified, please check usage MP4Box version 0.5.0-rev4065 – Roman Podlinov Oct 12 '13 at 19:05
  • 2
    Shouldn't it be something like `MP4Box -add fixed.mp4 -isma sample.mp4`? See http://muzso.hu/2012/11/14/tools-to-fix-mp4-videos-so-players-can-start-playback-instantly-without-downloading-the-w – Gwyneth Llewelyn Oct 20 '19 at 09:38
-6

Add to your gradle this lib: compile 'net.ypresto.qtfaststartjava:qtfaststart:0.1.0' and then

File input = new File(path + "/input.mp4"); // Your input file
File output = new File(path + "/output.mp4"); // Your output file
try{
    if(!output.exists()) // if there is no output file we'll create one
        output.createNewFile();
    }
}catch (IOException e){
    Log.e("TAG", e.toString());
}

try{
    QtFastStart.fastStart(input, output); // Adds moov to your input
                                          // Now your output file is ready to stream!
}catch (QtFastStart.MalformedFileException m){
    Log.e("QT", m.toString());
}catch (QtFastStart.UnsupportedFileException q){
    Log.e("QT", q.toString());
}catch (IOException i){
    Log.e("QT", i.toString());
}

Here that's all