当前位置: 首页 > 知识库问答 >
问题:

基于ffmpeg的Android视频裁剪

祁奇略
2023-03-14

我正在使用https://github.com/writingminds/ffmpeg-android-java命令裁剪视频:

execFFmpegCommand(" -i " + in + " -filter:v " + "crop=" + 240 + ":" + 120 + ":" + 100 + ":" + 100 + " -c:a copy " + out);

其中“in”是inputfile,“out”是outputfile。

execFFmpegCommand(" -i " + in + " -filter:v " + "crop=" + 240 + ":" + 120 + ":" + 100 + ":" + 100 + " -c:a copy " + out);
private void execFFmpegCommand(final String command) {
    try {
        ffmpeg.execute(new String[]{command}, new ExecuteBinaryResponseHandler() {
            @Override
            public void onFailure(String s) {
                Log.d(TAG, "FAILED with output : " + s);
                progressDialog.dismiss();

            }

            @Override
            public void onSuccess(String s) {
                Log.d(TAG, "SUCCESS with output : " + s);
            }

            @Override
            public void onProgress(String s) {
                Log.d(TAG, "Started command : ffmpeg " + command);
                Log.d(TAG, "progress : " + s);
            }

            @Override
            public void onStart() {
                Log.d(TAG, "Started command : ffmpeg " + command);
                progressDialog.show();
            }

            @Override
            public void onFinish() {
                Log.d(TAG, "Finished command : ffmpeg " + command);
                progressDialog.dismiss();

            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // do nothing for now
    }
}
 D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.209 17283-17866/com.myapp D/FFmpeg: Running publishing updates method
08-12 11:16:03.231 17283-17283/com.myapp W/MediaRecorder: mediarecorder went away with unhandled events
08-12 11:16:03.232 17283-17283/com.myapp W/MediaRecorder: mediarecorder went away with unhandled events
08-12 11:16:03.355 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.355 17283-17283/com.myapp D/ffmpeg: progress : ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
08-12 11:16:03.355 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.355 17283-17283/com.myapp D/ffmpeg: progress :   built with gcc 4.8 (GCC)
08-12 11:16:03.355 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.355 17283-17283/com.myapp D/ffmpeg: progress :   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=
08-12 11:16:03.355 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress :   libavutil      55. 17.103 / 55. 17.103
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress :   libavcodec     57. 24.102 / 57. 24.102
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress :   libavformat    57. 25.100 / 57. 25.100
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress :   libavdevice    57.  0.101 / 57.  0.101
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress :   libavfilter     6. 31.100 /  6. 31.100
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress :   libswscale      4.  0.100 /  4.  0.100
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress :   libswresample   2.  0.101 /  2.  0.101
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress :   libpostproc    54.  0.100 / 54.  0.100
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress : Output #0, mp4, to ' -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4':
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: Started command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4
08-12 11:16:03.356 17283-17283/com.myapp D/ffmpeg: progress : Output file #0 does not contain any stream
08-12 11:16:03.357 17283-17283/com.myapp D/ffmpeg: FAILED with output : 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
                                                          Output #0, mp4, to ' -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4':
                                                          Output file #0 does not contain any stream
08-12 11:16:03.370 17283-17283/com.myapp D/ffmpeg: Finished command : ffmpeg  -i /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980754658.mp4 -vf crop=240:120:100:100 -c:a copy /storage/emulated/0/Android/data/com.myapp/Files/Video/myapp1470980763106_vid.mp4

共有1个答案

宦书
2023-03-14

我终于想通了,命令应该作为数组传递。

  String[] cmd = {"-i", in, "-filter:v", "crop=" + 240 + ":" + 120 + ":" + 100 + ":" + 100, "-c:a", "copy", out};

    execFFmpegCommand(cmd);
 类似资料:
  • 我尝试使用以下命令使用视频的开始和结束时间来剪切视频 通过使用上述命令,我想将视频从剪切到。但它并不是在这两个时间之间剪掉视频,而是剪掉前11秒的视频。有谁能帮我解决这个问题吗? 编辑1: 我尝试通过使用以下命令来剪切,这是由mark4o建议的 但显示出以下错误。 编码器'AAC'是实验性的,但未启用实验性编解码器 因此我将添加到命令中, 现在一切正常。

  • 问题:是否有一个单独的FFMPEG裁剪过滤器可以用于所有视频,包括(但不限于):856x480、640x480和1280x720? 我们有一个视频处理系统(使用DirectShow),所有输入到这个系统的视频必须是16:9使用MJPEG编解码器。我们使用下面的ffmpeg命令将源视频转换为MJPEG,缩放像素使其为正方形,然后裁剪到16:9的纵横比。这对大多数输入视频都很管用,输出正是我们想要的。

  • 我需要组合2视频垂直或水平。但在此之前,我需要裁剪一两个视频。 两种视频大小都需要为720x640。我需要组合2个分辨率为720x1280的视频。我首先将它们裁剪到720x640(从顶部裁剪320px,从底部裁剪320px),然后垂直组合。 我可以组合相同大小的视频与命令: ffmpeg-i1.mp4-i2.mp4-filter_complex"[0: v]比例=520:-1[v0];[1: v]

  • 我需要使用FFmpeg切割视频,我不能转码原始视频(因为性能的原因)。我在iPhone上的HEVC视频中遇到了一个问题:剪切视频的开头有滑移。 以下是我们在发行前如何转换视频: ffmpeg.exe-i original.mov-c:v copy-c:a aac-ss 4-y good.mp4 经过一些Googlen搜索,发现输入前的选项更快,但不太准确,而输入后和输出前的选项更慢,但更准确。 所

  • 我正在使用ffmpeg录制一个视频使用覆盆子Pi与它的相机模块。我想运行一个图像分类器在一个规则的间隔,我需要从流中提取一个帧。 这是我当前用于录制的命令: 在其他线程中,建议使用此命令: 我不认为这是用于仍然附加到的文件,我得到错误“不能使用-SSEOF,test.h264的持续时间未知”。 ffmpeg允许这样做吗?

  • 我想知道是否有办法在Xamarin中裁剪视频。我似乎找不到任何例子。我试图查看现有的函数和类,但什么也找不到。 基本上制作方视频,比如Vine和Instagram的视频。我认为这是通过裁剪视频的其余部分来实现的,而不仅仅是放大。