使用 ffmpeg 将位图数组转换为视频
Array of Bitmaps into Video with ffmpeg
我正面临当前的问题。
我有一个包含 100 个位图的数组。这些是我从视图中截取的屏幕截图。
我使用 JCodec 将其制作成视频,但速度很慢。我希望使用 FFmpeg
获得更好的结果
现在我想使用 FFmpeg 库。有人问了类似的问题,但我不知道如何使用 ffmpeg 以及如何在我的特定情况下使用它。我看到的都是奇怪的复杂命令见:
File dir = your directory where image stores;
String filePrefix = "picture"; //imagename prefix
String fileExtn = ".jpg";//image extention
filePath = dir.getAbsolutePath();
File src = new File(dir, filePrefix + "%03d" + fileExtn);// image name should ne picture001, picture002,picture003 soon ffmpeg takes as input valid
complexCommand = new String[]{"-i", src + "", "-c:v", "libx264", "-c:a", "aac", "-vf", "setpts=2*PTS", "-pix_fmt", "yuv420p", "-crf", "10", "-r", "15", "-shortest", "-y", "/storage/emulated/0/" + app_name + "/Video/" + app_name + "_Video" + number + ".mp4"};
问题是,在这种情况下,他使用的是路径。我需要它来自数组。而且我不知道如何处理字符串(ComplexCommands):/
我的位图是这样的:
位图[]位图=新位图[100];
稍后补上。
如果有人正在搜索如何使用 JCodec 进行此操作:
try {
out = NIOUtils.writableFileChannel( Environment.getExternalStorageDirectory().getAbsolutePath()+"/***yourpath***/output"+System.currentTimeMillis()+".mp4");
// for Android use: AndroidSequenceEncoder
AndroidSequenceEncoder encoder = new AndroidSequenceEncoder(out, Rational.R(25, 1));
for (int i = 0 ; i < 100 ; i++) {
// Generate the image, for Android use Bitmap
// Encode the image
System.out.println("LOO2P"+i);
encoder.encodeImage(bitmaps[i]);
}
// Finalize the encoding, i.e. clear the buffers, write the header, etc.
encoder.finish();
} catch (FileNotFoundException e) {
System.out.println("fNF");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOE");
e.printStackTrace();
} finally {
System.out.println("IOSSE");
NIOUtils.closeQuietly(out);
}
根据 the ffmpeg wiki,您描述的方式正是它打算如何完成的。要实现这一点,您应该根据描述的命名约定将阵列中的每个图像写入单个文件。我不知道这是否有助于解决您的性能问题。
可以按照 this answer:
将图像写入文件
for (int i = 0; i<bitmapArray.length(); i++){
// Write to file while incrementing the file name
writeToFile(bitmapArray[i], i);
}
据我所知,ffmpeg 不支持直接从数组编码。
我正面临当前的问题。 我有一个包含 100 个位图的数组。这些是我从视图中截取的屏幕截图。 我使用 JCodec 将其制作成视频,但速度很慢。我希望使用 FFmpeg
获得更好的结果现在我想使用 FFmpeg 库。有人问了类似的问题,但我不知道如何使用 ffmpeg 以及如何在我的特定情况下使用它。我看到的都是奇怪的复杂命令见:
File dir = your directory where image stores;
String filePrefix = "picture"; //imagename prefix
String fileExtn = ".jpg";//image extention
filePath = dir.getAbsolutePath();
File src = new File(dir, filePrefix + "%03d" + fileExtn);// image name should ne picture001, picture002,picture003 soon ffmpeg takes as input valid
complexCommand = new String[]{"-i", src + "", "-c:v", "libx264", "-c:a", "aac", "-vf", "setpts=2*PTS", "-pix_fmt", "yuv420p", "-crf", "10", "-r", "15", "-shortest", "-y", "/storage/emulated/0/" + app_name + "/Video/" + app_name + "_Video" + number + ".mp4"};
问题是,在这种情况下,他使用的是路径。我需要它来自数组。而且我不知道如何处理字符串(ComplexCommands):/ 我的位图是这样的:
位图[]位图=新位图[100];
稍后补上。
如果有人正在搜索如何使用 JCodec 进行此操作:
try {
out = NIOUtils.writableFileChannel( Environment.getExternalStorageDirectory().getAbsolutePath()+"/***yourpath***/output"+System.currentTimeMillis()+".mp4");
// for Android use: AndroidSequenceEncoder
AndroidSequenceEncoder encoder = new AndroidSequenceEncoder(out, Rational.R(25, 1));
for (int i = 0 ; i < 100 ; i++) {
// Generate the image, for Android use Bitmap
// Encode the image
System.out.println("LOO2P"+i);
encoder.encodeImage(bitmaps[i]);
}
// Finalize the encoding, i.e. clear the buffers, write the header, etc.
encoder.finish();
} catch (FileNotFoundException e) {
System.out.println("fNF");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOE");
e.printStackTrace();
} finally {
System.out.println("IOSSE");
NIOUtils.closeQuietly(out);
}
根据 the ffmpeg wiki,您描述的方式正是它打算如何完成的。要实现这一点,您应该根据描述的命名约定将阵列中的每个图像写入单个文件。我不知道这是否有助于解决您的性能问题。 可以按照 this answer:
将图像写入文件for (int i = 0; i<bitmapArray.length(); i++){
// Write to file while incrementing the file name
writeToFile(bitmapArray[i], i);
}
据我所知,ffmpeg 不支持直接从数组编码。