Android FFMPEG 不起作用

3

我尝试使用FFMPEG在Android设备上缩小视频。

在调用scaleVideo()的Activity的OnCreate中,还有FFMPEG和二进制文件的初始化加载。

private void scaleVideo(String path) throws FFmpegCommandAlreadyRunningException, IOException {
    File copy = new File(DESTINATION_PATH + FilenameUtils.getBaseName(path) + "_scaled." + FilenameUtils.getExtension(path));

    if (!copy.exists())
        copy.createNewFile();

    String cmd = "-i " + path + " -vf scale=320:-1 " + copy.getPath();
    String[] command = cmd.split(" ");
    fFmpeg.execute(command, new FFmpegExecuteResponseHandler() {
        @Override
        public void onSuccess(String message) {
            Log.i("FFmpeg", message);
            mProgressDialog.cancel();

            runOnUiThread(() -> Toast.makeText(TrimmerActivity.this, getString(R.string.video_saved_at, copy.getPath()), Toast.LENGTH_SHORT).show());

            VideoModel videoModel = new VideoModel();
            videoModel.setPath(copy.getPath());
            videoModel.save();

            finish();
        }

        @Override
        public void onProgress(String message) {
            Log.i("FFmpeg", message);
        }

        @Override
        public void onFailure(String message) {
            Log.e("FFmpeg", message);
        }

        @Override
        public void onStart() {
            Log.i("FFmpeg", "on start");
        }

        @Override
        public void onFinish() {
            Log.i("FFmpeg", "on finish");
        }
    });
}

我得到的输出如下:
I/FFmpeg: on start
D/FFmpeg: Running publishing updates method
I/FFmpeg: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
I/FFmpeg:   built with gcc 4.8 (GCC)
I/FFmpeg:   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=
I/FFmpeg:   libavutil      55. 17.103 / 55. 17.103
I/FFmpeg:   libavcodec     57. 24.102 / 57. 24.102
I/FFmpeg:   libavformat    57. 25.100 / 57. 25.100
I/FFmpeg:   libavdevice    57.  0.101 / 57.  0.101
I/FFmpeg:   libavfilter     6. 31.100 /  6. 31.100
I/FFmpeg:   libswscale      4.  0.100 /  4.  0.100
I/FFmpeg:   libswresample   2.  0.101 /  2.  0.101
I/FFmpeg:   libpostproc    54.  0.100 / 54.  0.100
I/FFmpeg: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/MP4_20161015_001403.mp4':
I/FFmpeg:   Metadata:
I/FFmpeg:     major_brand     : iso6
I/FFmpeg:     minor_version   : 1
I/FFmpeg:     compatible_brands: mp42iso6avc1isom
I/FFmpeg:     creation_time   : 2016-10-14 21:14:03
I/FFmpeg:   Duration: 00:00:02.10, start: 0.000000, bitrate: 19564 kb/s
I/FFmpeg:     Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080, 19403 kb/s, SAR 1:1 DAR 16:9, 14.75 fps, 15 tbr, 90k tbn, 180k tbc (default)
I/FFmpeg:     Metadata:
I/FFmpeg:       rotate          : 90
I/FFmpeg:       creation_time   : 2016-10-14 21:13:52
I/FFmpeg:     Side data:
I/FFmpeg:       displaymatrix: rotation of -90.00 degrees
I/FFmpeg:     Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 156 kb/s (default)
I/FFmpeg:     Metadata:
I/FFmpeg:       creation_time   : 2016-10-14 21:13:52

视频输出和输入正确。文件存在且视频播放器能够识别输入。 这就是全部。CPU/GPU/内存不做任何事情,也没有任何回调被调用。 这是我第一次使用FFMPEG,所以我不知道该怎么做。


你应该展示实际使用的 ffmpeg 命令。 - llogan
2个回答

4

我知道我有点晚了,但问题在于你试图将输出写入的文件已经存在。如果其他人也遇到这个问题,只需删除该文件或添加-y参数。这样可以覆盖现有文件。


为什么在这种情况下onFailure没有被调用? - Tenfour04

1

因此,这里提供了一种编程解决方案,可以将视频缩小到任何大小并将其保存为GIF格式。

private void scaleVideo(String path) throws FFmpegCommandAlreadyRunningException, IOException {
    File originalVideo = new File(path);
    String filename = DESTINATION_PATH + FilenameUtils.getBaseName(path) + "_scaled."
            + "gif";
    File copy = new File(filename);
    int i = 0;
    while (copy.exists()) {
        copy = new File(String.format(Locale.getDefault(),
                "%s/%s_scaled_(%d).gif", DESTINATION_PATH, FilenameUtils.getBaseName(path), i));
        i++;
    }
    copy.createNewFile();
    String cmd = "-y -v debug -i " + path + " -r 15 -vf scale=w=320:h=320:force_original_aspect_ratio=increase -threads 2 -gifflags +transdiff -y " + copy.getPath();
    String[] command = cmd.split(" ");
    File finalCopy = copy;
    fFmpeg.execute(command, new FFmpegExecuteResponseHandler() {
        @Override
        public void onSuccess(String message) {
            Log.i("FFmpeg", message);
        }
        @Override
        public void onProgress(String message) {
            String[] strings = message.split("\n");
            for (String string : strings) {
                Log.i("FFmpeg", string);
            }
        }
        @Override
        public void onFailure(String message) {
            String[] strings = message.split("\n");
            for (String string : strings) {
                Log.e("FFmpeg", string);
            }           
        }
        @Override
        public void onStart() {
            Log.i("FFmpeg", "on start");
        }
        @Override
        public void onFinish() {
            Log.i("FFmpeg", "on finish");
        }
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接