使用javacv和ffmpeg在Android中修剪视频长度

时间:2017-07-10 16:04:18

标签: android video ffmpeg javacv

我试图通过ffmpeg实现FrameGrabber和FrameRecorder来修剪视频长度,但是获得了更小尺寸的损坏文件然后它会成为。也许还有其他方法可以从开始时间到结束时间修剪视频,还可以更新修剪进度。似乎它没有记录帧之间的变化。也许有其他方法可以修剪不同格式的视频,如mp4,flv等。这是代码片段:

        FrameGrabber grabber = new FFmpegFrameGrabber(mClip.getPath());
        grabber.start();
        grabber.setTimestamp(mClip.getClipStartMs()); // Write from specific moment

        File out = new File(mClip.getOutPutPath(params[0])); // Set destination to write
        FrameRecorder recorder = new FFmpegFrameRecorder(out, grabber.getImageWidth(), grabber.getImageHeight());


        recorder.setFormat(grabber.getFormat());
        recorder.setFrameRate(grabber.getFrameRate());
        recorder.setSampleRate(grabber.getSampleRate());
        recorder.setAspectRatio(grabber.getAspectRatio());
        recorder.setSampleFormat(grabber.getSampleFormat());

        recorder.setAudioCodec(grabber.getAudioCodec());
        recorder.setAudioBitrate(grabber.getAudioBitrate());
        recorder.setAudioChannels(grabber.getAudioChannels());

        recorder.setVideoCodec(grabber.getVideoCodec());
        recorder.setVideoBitrate(grabber.getVideoBitrate());

        recorder.start();

        Frame frame;
        Long timestamp;
        Long fullLength = mClip.getClipEndMs() - mClip.getClipStartMs();
        double percent = 0d, oldPercent = 0d;

        while ((frame = grabber.grabFrame()) != null && (timestamp = grabber.getTimestamp()) <= mClip.getClipEndMs()) {
            Log.d(ASYNC_SAVE_TAG, "Started command : ffmpeg " + mClip.toString());

            if (timestamp != 0d) {
                oldPercent = percent;
                percent = timestamp.doubleValue() / fullLength.doubleValue();
                if (MathUtil.compare(percent, oldPercent) != 0) {
                    publishProgress(percent);
                }
            }

            recorder.setTimestamp(grabber.getTimestamp() - mClip.getClipStartMs());
            recorder.record(frame);
        }

        grabber.close();
        recorder.close();

1 个答案:

答案 0 :(得分:1)

您可以使用ffmpeg包装器(https://github.com/WritingMinds/ffmpeg-android-java),然后使用命令行ffmpeg trim方法。

包装器的优点是它应该隐藏了集成ffmpeg的大部分复杂性,并且它还允许您使用命令行语法并利用所有常见的ffmpeg讨论和支持。

然后您可以使用常规的ffmpeg trim语法:

  

ffmpeg -i INPUT -vf trim = 60:120

Morte信息包括上面的例子: