0

I am trying to wirte metadata information using mp4parser but in my code I am getting userDataBox Empty in case of video captured by android but in case of other video (I have tested with downloaded vide) it is not empty and I was added metadata successfully, my problem is for video captured by android having empty userdatabox. Can any body help me ?

        moov.getBoxes(UserDataBox.class).size()

My Code is here :

    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
            "MYFOLDER");
    File f = new File(mediaStorageDir, "VID.mp4");
    if(f.exists())
    {
        Toast.makeText(MainActivity.this," file found",Toast.LENGTH_SHORT).show();
    }

    try {
        fc = new FileInputStream(f).getChannel();

        // fc = new FileInputStream(f).getChannel();
        isoFile = new IsoFile(fc);
        String str = f.getAbsolutePath();
        MovieBox moov = isoFile.getMovieBox();
        // for (Box box : moov.getBoxes()) {
        // System.out.println("box" + box);
        // }



        if(moov.getBoxes(UserDataBox.class).size()>0)
        {
            UserDataBox udta = moov.getBoxes(UserDataBox.class).get(0); 



        }else{

}

Jitendra Prajapati
  • 1,002
  • 1
  • 10
  • 33

1 Answers1

0

If the udta (User Data Box) is not there you can create it. You might want to have a look at the ChangeMetadata example on github.

UserDataBox userDataBox;
long sizeBefore;
if ((userDataBox = Path.getPath(tempIsoFile, "/moov/udta")) == null) {
   sizeBefore = 0;
   userDataBox = new UserDataBox();
   tempIsoFile.getMovieBox().addBox(userDataBox);
} else {
   sizeBefore = userDataBox.getSize();
}
MetaBox metaBox;
if ((metaBox = Path.getPath(userDataBox, "meta")) == null) {
   metaBox = new MetaBox();
   userDataBox.addBox(metaBox);
}


XmlBox xmlBox = new XmlBox();
xmlBox.setXml(text);
metaBox.addBox(xmlBox);

now you have added the boxes. Unfortunately the file contains other boxes that reference the actual video/audio samples. These reference are absolute to the beginning of the file and must be adjusted as you might have inserted data between the start of the file and the actual audio/video samples.

The needsOffsetCorrection(...) method checks if the data was really inserted between filestar and samples. correctChunkOffsets(...) then does he actual correction of the offsets stored in the stco (ChunkOffsetBox).

long sizeAfter = userDataBox.getSize();
if (needsOffsetCorrection(tempIsoFile)) {
    correctChunkOffsets(tempIsoFile, sizeAfter - sizeBefore);
}

videoFileOutputStream = new FileOutputStream(videoFilePath + "_mod.mp4");
tempIsoFile.getBox(videoFileOutputStream.getChannel());

I hope that helps you a bit understanding how MP4 and metadata in MP4 is working. Good Luck!

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