Cheloute
Cheloute

Reputation: 903

I get a non playable MP4 file when created with Mp4Parser Java API

I'm studying Mp4Parser API (Mp4Parser GitHub) and try to learn how it's working. I first tried to create an MP4 by "copying" the highest level tags into a new file. If I get a similar file, it's unfortunately not "the same file" and result can't be played.

My dirty (it's just a quick try) code is:

public void copy(String videoFilePath) throws IOException {
        File videoFile = new File(videoFilePath);
        File videoPro2 = new File("/tmp/output.mp4");

        if (videoPro2.exists()) 
           videoPro2.delete();
        videoPro2.createNewFile();

        FileOutputStream fos = new FileOutputStream(videoPro2);
        
        IsoFile isoFile = new IsoFile(new FileInputStream(videoFilePath).getChannel());
        IsoFile pro2 = new IsoFile(new FileInputStream(videoPro2).getChannel());
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        pro2.addBox(Path.getPath(isoFile, "ftyp[0]"));
        pro2.addBox(Path.getPath(isoFile, "free[0]"));
        pro2.addBox(Path.getPath(isoFile, "moov[0]"));
        pro2.addBox(Path.getPath(isoFile, "mdat[0]"));
        pro2.getBox(Channels.newChannel(baos));
         
        baos.writeTo(fos);
        
        isoFile.close();
        pro2.close();
        baos.close();
        
        return;
    }

I tested my original MP4 with AtomicParsley, saw there were in that order, a ftyp, a free, a moov and an mdat tags.

My method basically aims to get these four tags and puts them into the destination file to "make an exact copy". Or that's what I was expecting, but it's not the case.

Firstly, output.mp4 is not playable, but comparing the hex dumps of input and output... almost everything is different, except ftyp and free. Why?

I can see that tag size are not similar neither..

Obviously, my goal is not to "copy" files, but understand Mp4Parser API as I'd like to use it in a real project. But this copy method is a starter for me to understand how this works, as I'm not familiar with MP4 specification.

Thanks

Upvotes: 0

Views: 99

Answers (1)

Cheloute
Cheloute

Reputation: 903

Ok, my fault. There was an UUID tag I missed in the original footage between moov and mdat tags. Copying the UUID tag using

pro2.addBox(Path.getPath(isoFile, "uuid[0]"));

just before adding the mdat atom does the trick.

Sorry for this :)

Upvotes: 0

Related Questions