要在Android应用中裁剪视频,我使用ffmpeg库
在SO example中,我找到了以下命令裁剪视频:
ffmpeg -i in.mp4 -filter:v" crop = out_w:out_h:x:y" out.mp4
我创建了一个String数组:
String [] cmd = {" ffmpeg"," -i",originalVideoPath," -vf"," \"裁剪=" + imageWidth +":" + imageHeight +":" + xCoordinate +":" + yCoordinate +" \""," -c:a"," copy",croppedVideoPath};
我将此数组传递给execute()方法:
try {
ffmpeg = FFmpeg.getInstance(getContext());
ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
@Override
public void onStart() {
Log.i(TAG, "onStart: Crop Video started");
}
@Override
public void onProgress(String message) {
Log.i(TAG, "onProgress: " + message);
}
@Override
public void onFailure(String message) {
Log.i(TAG, "onFailure: " + message);
}
@Override
public void onSuccess(String message) {
Log.i(TAG, "onSuccess: " + message);
}
@Override
public void onFinish() {
Log.i(TAG, "onFinish: Video cropping finished");
}
});
} catch (FFmpegCommandAlreadyRunningException e) {
// Handle if FFmpeg is already running
Log.i(TAG, "doInBackground: Exception + Device is not supported");
}
日志结果是:
12-14 11:33:33.246 5731-5731/com.studioidan.dmc I/CropImageFragment: onStart: Crop Video started
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: built with gcc 4.8 (GCC)
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: libavutil 55. 17.103 / 55. 17.103
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: libavcodec 57. 24.102 / 57. 24.102
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: libavformat 57. 25.100 / 57. 25.100
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: libavdevice 57. 0.101 / 57. 0.101
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: libavfilter 6. 31.100 / 6. 31.100
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: libswscale 4. 0.100 / 4. 0.100
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: libswresample 2. 0.101 / 2. 0.101
12-14 11:33:33.246 5731-5731/dmc I/CropImageFragment: onProgress: libpostproc 54. 0.100 / 54. 0.100
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DMC/diamondVideo.mp4':
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: Metadata:
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: major_brand : isom
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: minor_version : 0
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: compatible_brands: isom3gp4
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: creation_time : 2016-12-14 08:33:10
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: Duration: 00:00:05.14, start: 0.000000, bitrate: 13480 kb/s
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 13824 kb/s, SAR 1:1 DAR 16:9, 29.96 fps, 30 tbr, 90k tbn, 180k tbc (default)
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: Metadata:
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: creation_time : 2016-12-14 08:33:10
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: handler_name : VideoHandle
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default)
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: Metadata:
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: creation_time : 2016-12-14 08:33:10
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: handler_name : SoundHandle
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: [NULL @ 0x433f5f40] Unable to find a suitable output format for 'ffmpeg'
12-14 11:33:33.316 5731-5731/dmc I/CropImageFragment: onProgress: ffmpeg: Invalid argument
12-14 11:33:33.326 5731-5731/dmc I/CropImageFragment: onFailure: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8 (GCC)
configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
libavutil 55. 17.103 / 55. 17.103
libavcodec 57. 24.102 / 57. 24.102
libavformat 57. 25.100 / 57. 25.100
libavdevice 57. 0.101 / 57. 0.101
libavfilter 6. 31.100 / 6. 31.100
libswscale 4. 0.100 / 4. 0.100
libswresample 2. 0.101 / 2. 0.101
libpostproc 54. 0.100 / 54. 0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DMC/diamondVideo.mp4':
Metadata:
major_brand : isom
minor_version : 0
compatible_brands: isom3gp4
creation_time : 2016-12-14 08:33:10
Duration: 00:00:05.14, start: 0.000000, bitrate: 13480 kb/s
Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 13824 kb/s, SAR 1:1 DAR 16:9, 29.96 fps, 30 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2016-12-14 08:33:10
handler_name : VideoHandle
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default)
Metadata:
creation_time : 2016-12-14 08:33:10
handler_name : SoundHandle
[NULL @ 0x433f5f40] Unable to find a suitable output format for 'ffmpeg'
ffmpeg: Invalid argument
12-14 11:33:33.326 5731-5731/dmc I/CropImageFragment: onFinish: Video cropping finished
但是当我从命令数组
中删除ffmpeg时String [] cmd = {" -i",originalVideoPath," -vf"," \" crop =" + imageWidth +":" + imageHeight +":" + xCoordinate +":" + yCoordinate +" \""," -c:a"," copy",croppedVideoPath};
执行后,结果为:
12-14 11:56:46.366 25930-26592/dmc I/CropImageFragment: onStart: Crop Video started
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: built with gcc 4.8 (GCC)
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: libavutil 55. 17.103 / 55. 17.103
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: libavcodec 57. 24.102 / 57. 24.102
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: libavformat 57. 25.100 / 57. 25.100
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: libavdevice 57. 0.101 / 57. 0.101
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: libavfilter 6. 31.100 / 6. 31.100
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: libswscale 4. 0.100 / 4. 0.100
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: libswresample 2. 0.101 / 2. 0.101
12-14 11:56:46.426 25930-25930/dmc I/CropImageFragment: onProgress: libpostproc 54. 0.100 / 54. 0.100
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DMC/diamondVideo.mp4':
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: Metadata:
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: major_brand : isom
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: minor_version : 0
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: compatible_brands: isom3gp4
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: creation_time : 2016-12-14 08:56:40
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: Duration: 00:00:05.27, start: 0.000000, bitrate: 13189 kb/s
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 13886 kb/s, SAR 1:1 DAR 16:9, 30.01 fps, 30 tbr, 90k tbn, 180k tbc (default)
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: Metadata:
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: creation_time : 2016-12-14 08:56:40
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: handler_name : VideoHandle
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default)
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: Metadata:
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: creation_time : 2016-12-14 08:56:40
12-14 11:56:46.506 25930-25930/dmc I/CropImageFragment: onProgress: handler_name : SoundHandle
看起来这个过程开始了,之后冻结了
我做错了什么?
答案 0 :(得分:2)
我自己的实施:
private void cropVideo(String originalVideoPath, String croppedVideoPath, int xCoordinate,
int yCoordinate, int imageWidth, int imageHeight, int croppedAngle) {
String command;
ffmpegCommand = new StringBuilder();
ffmpegCommand.append("-y");
ffmpegCommand.append(" ");
ffmpegCommand.append("-i");
ffmpegCommand.append(" ");
ffmpegCommand = getFFmpegCommand(ffmpegCommand, imageWidth, imageHeight,
xCoordinate, yCoordinate, originalVideoPath, croppedVideoPath, croppedAngle);
command = ffmpegCommand.toString();
Log.i(TAG, command);
String[] cmd = command.split(" ");
new CropVideoTask().execute(cmd);
}
private StringBuilder getFFmpegCommand(StringBuilder ffmpegCommand, int imageWidth,
int imageHeight, int xCoordinate, int yCoordinate,
String originalVideoPath, String croppedVideoPath, int angle) {
int x = 0;
int y = 0;
int w = 0;
int h = 0;
String rotate = "";
switch (angle) {
case 0:
x = xCoordinate;
y = yCoordinate;
w = imageWidth;
h = imageHeight;
break;
case 90:
x = yCoordinate;
y = mFileWidth - imageWidth - xCoordinate;
w = imageHeight;
h = imageWidth;
rotate = ",transpose=1";
break;
case 180:
x = mFileHeight - imageWidth - xCoordinate;
y = mFileWidth - imageHeight - yCoordinate;
w = imageWidth;
h = imageHeight;
rotate = ",vflip";
break;
case -90:
x = mFileHeight - imageHeight - yCoordinate;
y = xCoordinate;
w = imageHeight;
h = imageWidth;
rotate = ",transpose=2";
break;
}
ffmpegCommand.append(originalVideoPath);
ffmpegCommand.append(" ");
ffmpegCommand.append("-vf");
ffmpegCommand.append(" ");
ffmpegCommand.append("crop=");
ffmpegCommand.append(w);
ffmpegCommand.append(":");
ffmpegCommand.append(h);
ffmpegCommand.append(":");
ffmpegCommand.append(x);
ffmpegCommand.append(":");
ffmpegCommand.append(y);
ffmpegCommand.append(rotate);
ffmpegCommand.append(" ");
ffmpegCommand.append(croppedVideoPath);
return ffmpegCommand;
}
private class CropVideoTask extends AsyncTask<String[], Void, Void> {
protected Void doInBackground(String[]... cmd) {
try {
ffmpeg = FFmpeg.getInstance(getContext());
ffmpeg.execute(cmd[0], new ExecuteBinaryResponseHandler() {
@Override
public void onStart() {
}
@Override
public void onProgress(String message) {
}
@Override
public void onFailure(String message) {
}
@Override
public void onSuccess(String message) {
// Do something with the cropped video
}
@Override
public void onFinish() {
}
});
} catch (FFmpegCommandAlreadyRunningException e) {
Log.i(TAG, "doInBackground: Exception + Device is not supported");
}
return null;
}
}