sannies / mp4parser

A Java API to read, write and create MP4 files
Apache License 2.0
2.76k stars 566 forks source link

Can not merge after FFmpeg cropping #269

Open lumyus opened 7 years ago

lumyus commented 7 years ago

Hey,

I am currently trying to merge two videos. Both are of .mp4 type and have originally two different resolutions. Now since mp4parser can only merge the same resolutions, I apply a cropping filter using FFMPEG.

This is my ffmpeg command: String[] cmd = {"-i", input_video.getAbsolutePath(), "-vf","crop=640:480", "-acodec", "copy", "-threads", "5", "-preset", "ultrafast" , output_video};

Error: `` Cannot merge AudioSampleEntry{bytesPerSample=0, bytesPerFrame=0, bytesPerPacket=0, samplesPerPacket=0, packetSize=0, compressionId=0, soundVersion=0, sampleRate=44100, sampleSize=16, channelCount=1, boxes=[com.googlecode.mp4parser.boxes.mp4.ESDescriptorBox@1a3]} and AudioSampleEntry{bytesPerSample=0, bytesPerFrame=0, bytesPerPacket=0, samplesPerPacket=0, packetSize=0, compressionId=0, soundVersion=0, sampleRate=44100, sampleSize=16, channelCount=2, boxes=[com.googlecode.mp4parser.boxes.mp4.ESDescriptorBox@fffff9bc]}`

This is the code I use to merge the two videos:


void mergeVideo(List<File> parts, File outFile) {
        try {
            Movie finalMovie = new Movie();
            List<Track> videoTracks = new LinkedList<>();
            List<Track> audioTracks = new LinkedList<>();

            for (int i = 0; i < parts.size(); i++) {
                String videoPath = parts.get(i).getPath();
                Movie movie = MovieCreator.build(videoPath);

                for (Track t : movie.getTracks()) {
                    if (t.getHandler().equals("vide")) {
                        videoTracks.add(t);
                    } else if (t.getHandler().equals("soun")) {
                        audioTracks.add(t);
                    }
                }
            }

            if (videoTracks.size() > 0) {
                finalMovie.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
            }

            if (audioTracks.size() > 0) {
                finalMovie.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
            }

            FileOutputStream fos = new FileOutputStream(outFile);
            BasicContainer container = (BasicContainer) new DefaultMp4Builder().build(finalMovie);
            container.writeContainer(fos.getChannel());

        } catch (IOException e) {
            Log.e(TAG, "Merge failed", e);
        }

        parts.clear();
    }

Does anyone have an idea whats going on here? Merging two videos without merging one of them when using the same resolutions works perfectly.

The "-acodec", "copy" part is because I tried to avoid any change in the audio, however this did not help either.

Cheers

sannies commented 7 years ago

You got a mono and a stereo soundtrack. Appending mono to stereo won't work!