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,