0

I am trying to remove audio from MP4 files, and then upload the muted version to an S3 bucket. I am doing this using MP4Parser. Some files work, and a muted version uploads as expected. Others upload, but are unviewable. Instead, there is an error screen that says "This video can't be watch because the file is corrupt." Files that work, always work, and files that don't work, never work. Here is the function that removes audio:

private static void removeAudioFromVideo(MovieBox movieBox, IsoFile isoFile,
                                          String filePath, String requestId) throws IOException {
     Path path = Paths.get(filePath);
     path = path.getFileName();
     String fileName = path.toString();

     List<TrackBox> trackBoxesToRemove = new ArrayList<>();
     for (TrackBox trackBox : movieBox.getBoxes(TrackBox.class)) {
         String handlerType = trackBox.getMediaBox().getHandlerBox().getHandlerType();
         if(handlerType.equals(AUDIO_HANDLER_TYPE)) {
             trackBoxesToRemove.add(trackBox);
         }
     }
     for (TrackBox trackBox : trackBoxesToRemove) {
         movieBox.getBoxes().remove(trackBox);
     }
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
     WritableByteChannel byteChannel = Channels.newChannel(outputStream);
     isoFile.getBox(byteChannel);

     byte[] mutedFileBytes = outputStream.toByteArray();
     ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mutedFileBytes);
     ObjectMetadata objectMetadata = new ObjectMetadata();
     objectMetadata.setContentLength(mutedFileBytes.length);

     s3Client.putObject(INPUT_BUCKET_NAME, "input/HFE/"+ requestId
             + "/refinedVideo/refined_" + fileName, byteArrayInputStream, objectMetadata);
     byteArrayInputStream.close();
     byteChannel.close();
     outputStream.close();
 }

I know the issue is related to audio removal, because if I remove audio removal, all files get uploaded successfully. The audio is removed in these lines:

 for (TrackBox trackBox : trackBoxesToRemove) {
     movieBox.getBoxes().remove(trackBox);
 }

I am not sure how else I can remove audio. It seems like it could be a file issue. I have tried looking at some metadata with FFMPEG, but nothing jumps out as an obvious difference between files that work and files that don't. I'm also open to other Java libraries for this (preferably not ffmpeg, I am having dependency issue), or a different way to remove audio using MP4Parser,

  • *(preferably not ffmpeg, I am having dependency issue)* Can you expand? – g00se Mar 14 '23 at 23:26
  • This is being done in an AWS lambda function. I understand that I have to include native libraries in order to use ffmpeg with Java. In addition, I saw different libraries were needed for different platforms using the app. I don't know exactly how many platforms will use our app. And in any case, I have been having a hard time getting the native libraries included in the lambda file directory – mourningraptor3 Mar 15 '23 at 00:36
  • @mourningraptor3 If the problem is a "not working file" why not share a link to a small example? If you can be told what's wrong with the output video, then you can also know how to fix your code to (possibly) stop the issue – VC.One Mar 18 '23 at 20:09

0 Answers0