使用 FFMPEG android java 预编译库更改视频播放确实是一个非常缓慢的过程
Changing Video play back is really very slow process using FFMPEG android java pre compiled library
我正在使用 FFMPEG android java 库处理 android 中的视频以改变播放速度。对于 6 秒的视频,使其播放速度慢 0.5 setpts,它需要超过 1 分钟。
这是我的代码
public class TestFFMpegActivity {
private static String cmd,
private static FFmpeg ffmpeg;
private static Context mContext;
public static String getInternalDirectoryPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
public static void initiateFFmpeg(Context context, String path) {
mContext = context;
ffmpeg = FFmpeg.getInstance(context);
VideoIn = getInternalDirectoryPath() + "/Download/input.mp4";
VideoOut = getInternalDirectoryPath() + "/Download/output.mp4";
cmd = "-i "+VideoIn+" -vf setpts=2*PTS -strict -2 "+VideoOut;
try {
ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
@Override
public void onStart() {
}
@Override
public void onFailure() {
}
@Override
public void onSuccess() {
}
@Override
public void onFinish() {
processVideo();
}
});
} catch (FFmpegNotSupportedException e) {
// Handle if FFmpeg is not supported by device
}
}
private static void processVideo(){
try {
ffmpeg.execute(cmd ,
new ExecuteBinaryResponseHandler() {
@Override
public void onStart() {
//for logcat
Log.w(null,"processing started");
}
@Override
public void onProgress(String message) {
//for logcat
Log.w(null, "onProgress");
}
@Override
public void onFailure(String message) {
Log.w(null, message.toString());
}
@Override
public void onSuccess(String message) {
Log.w(null, message.toString());
}
@Override
public void onFinish() {
}
});
} catch (FFmpegCommandAlreadyRunningException e) {
Toast.makeText(mContext, "Video processing failed due to exception", Toast.LENGTH_LONG).show();
// Handle if FFmpeg is already running
e.printStackTrace();
Log.w(null, e.toString());
}
}
}
这是gradle使用上述库的构建路径
compile 'com.github.hiteshsondhi88.libffmpeg:FFmpegAndroid:0.2.5'
要更改播放命令,请在其中添加参数“-ultrafast”。
现在 cmd 会像
cmd= "-i " + VideoIn+ " -vf setpts=2*PTS -c:v libx264 -c:a aac -strict experimental -vcodec libx264 -preset ultrafast -b:a 128k " + VideoOut;
这个参数对加工有神奇的作用。在相同环境下,视频处理时间从 1 分 6 秒减少到 13 秒。
我正在使用 FFMPEG android java 库处理 android 中的视频以改变播放速度。对于 6 秒的视频,使其播放速度慢 0.5 setpts,它需要超过 1 分钟。 这是我的代码
public class TestFFMpegActivity {
private static String cmd,
private static FFmpeg ffmpeg;
private static Context mContext;
public static String getInternalDirectoryPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
public static void initiateFFmpeg(Context context, String path) {
mContext = context;
ffmpeg = FFmpeg.getInstance(context);
VideoIn = getInternalDirectoryPath() + "/Download/input.mp4";
VideoOut = getInternalDirectoryPath() + "/Download/output.mp4";
cmd = "-i "+VideoIn+" -vf setpts=2*PTS -strict -2 "+VideoOut;
try {
ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
@Override
public void onStart() {
}
@Override
public void onFailure() {
}
@Override
public void onSuccess() {
}
@Override
public void onFinish() {
processVideo();
}
});
} catch (FFmpegNotSupportedException e) {
// Handle if FFmpeg is not supported by device
}
}
private static void processVideo(){
try {
ffmpeg.execute(cmd ,
new ExecuteBinaryResponseHandler() {
@Override
public void onStart() {
//for logcat
Log.w(null,"processing started");
}
@Override
public void onProgress(String message) {
//for logcat
Log.w(null, "onProgress");
}
@Override
public void onFailure(String message) {
Log.w(null, message.toString());
}
@Override
public void onSuccess(String message) {
Log.w(null, message.toString());
}
@Override
public void onFinish() {
}
});
} catch (FFmpegCommandAlreadyRunningException e) {
Toast.makeText(mContext, "Video processing failed due to exception", Toast.LENGTH_LONG).show();
// Handle if FFmpeg is already running
e.printStackTrace();
Log.w(null, e.toString());
}
}
}
这是gradle使用上述库的构建路径
compile 'com.github.hiteshsondhi88.libffmpeg:FFmpegAndroid:0.2.5'
要更改播放命令,请在其中添加参数“-ultrafast”。 现在 cmd 会像
cmd= "-i " + VideoIn+ " -vf setpts=2*PTS -c:v libx264 -c:a aac -strict experimental -vcodec libx264 -preset ultrafast -b:a 128k " + VideoOut;
这个参数对加工有神奇的作用。在相同环境下,视频处理时间从 1 分 6 秒减少到 13 秒。