gpt4 book ai didi

android - JavaCV读取视频并记录

转载 作者:行者123 更新时间:2023-11-30 02:31:22 26 4
gpt4 key购买 nike

我正在尝试在 Android 上合并 mp4 和 mp3,我正在使用 JavaCV,请先检查我下面的代码,然后我解释了奇怪的错误:

private void test2() throws Exception {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath();

int testId = 4;
String videoPath = path + "/" + "sample" + testId + ".mp4";
String audioPath = path + "/" + "love.mp3";
String outputPath = path + "/" + "out" + testId + ".mp4";

FrameGrabber grabber1 = new FFmpegFrameGrabber(videoPath);
FrameGrabber grabber2 = new FFmpegFrameGrabber(audioPath);

grabber1.start();
grabber2.start();

FrameRecorder recorder = new FFmpegFrameRecorder(outputPath,
grabber1.getImageWidth(), grabber1.getImageHeight(),
grabber2.getAudioChannels());

double frameRate = grabber1.getFrameRate();
recorder.setFrameRate(frameRate);
recorder.setSampleRate(grabber2.getSampleRate());
recorder.setVideoQuality(1);

recorder.start();

Frame frame1, frame2 = null;

// getLengthInTime is correct, but getLengthInFrames not accurate.
Log.d(TAG, " Video lengthInTime:" + grabber1.getLengthInTime()
+ " Video frames:" + grabber1.getLengthInFrames());

// Record video.
int count = 0;
while (true) {
frame1 = grabber1.grabFrame();
if (frame1 == null) {
break;
}

frame1.samples = null;
recorder.record(frame1);
count++;

Log.d(TAG, "Video frame timestamp:" + grabber1.getTimestamp());
}
Log.d(TAG, " Video frame count:" + count);

// Record audtio.
long videoTimestamp = recorder.getTimestamp();
while (true) {
frame2 = grabber2.grabFrame();
if (frame2 != null && grabber2.getTimestamp() <= videoTimestamp) {
frame2.image = null;
recorder.record(frame2);

// Log.d(TAG, "Audio frame timestamp:" +
// grabber2.getTimestamp());
} else {
break;
}
}

// release
recorder.stop();
recorder.release();

grabber1.stop();
grabber2.stop();
}

输出的音频没问题,但视频很卡。视频播放1s,停止1s,然后这样重复。我用手机摄像头录制的输入视频。

我尝试计算视频帧的实际数量,发现实际数量比从方法 getLengthInFrames() 获得的数量大得多。

最佳答案

frame Grabber 同时抓取图像帧和声音帧

如果您需要处理 VideoFrames 或其计数,请这样做

Frame grabFrame = frameGrabber.grabFrame();
if (grabFrame == null) {
// all frames are processed
System.out.println("!!! Failed cvQueryFrame");
runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(RecordActivity.this, "Done !!!", Toast.LENGTH_SHORT).show();
}

});
break;
}
if (grabFrame.image != null) {
//This is a video Frame, Do your work here
}

}

关于android - JavaCV读取视频并记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27291794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com