ffmpeg从视频中提取帧

时间:2017-11-22 17:35:15

标签: android video ffmpeg

我正在使用Writingminds FFmpeg在Android上使用FFmpeg。我目前正在尝试从视频中提取帧。

我想从整个视频中均匀分布的视频中提取8帧。我发现这里tutorial是我的实现。

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(videoCroppedFile.getAbsolutePath());
    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    Long videoLength = Long.parseLong(time);


final String cmd[] = {

            "-i",
            videoCroppedFile.getAbsolutePath(),
            "-f",
            "image2",
            "-ss",
            String.valueOf(videoLength.intValue() / ((8 + 1) * 10)),
            "-r",
            String.valueOf((8 + 1) / videoLength.intValue()),
            mediaStorageDir.getAbsolutePath() + "/%d.jpg"
    };

但是,当我转到应该保存帧的文件夹时,那里没有任何东西。此外,没有错误消息。

我觉得这是这个库获取String参数的一种方式。我曾经坚持过这个问题,我曾尝试过很多不同版本的cmd。我希望有人可以提供帮助。

这是ffmpeg的输出:

1-22 19:33:14.904 30981-30981/com.firebase.android D/videoFrames:  failure reason 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 '/data/user/0/com.firebase.android/cache/videovgntp6q5ar4dglkaflaoobfpcv945824159.mp4':
         Metadata:
           major_brand     : isom
           minor_version   : 512
           compatible_brands: isomiso2avc1mp41
           encoder         : Lavf57.25.100
         Duration: 00:00:08.28, start: 0.000000, bitrate: 391 kb/s
           Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 720x764 [SAR 1:1 DAR 180:191], 132 kb/s, 16.67 fps, 16.67 tbr, 12800 tbn, 33.33 tbc (default)
           Metadata:
             handler_name    : VideoHandler
           Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
           Metadata:
             handler_name    : SoundHandler
       Invalid framerate value: 0

更新2

这是我的Android代码:

      try {
        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

            @Override
            public void onStart() {
                Log.d("videoFrames", "starting to load binary");
            }

            @Override
            public void onFailure() {
                Log.d("videoFrames", "failed to load binary");
            }

            @Override
            public void onSuccess() {
                Log.d("videoFrames", "loaded binary");
                try {
                    ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                        @Override
                        public void onStart() {
                            Log.d("videoFrames", " starting to get frames from video");
                            if (mediaStorageDir.isDirectory() && mediaStorageDir.list().length > 0) {
                                String[] children = mediaStorageDir.list();
                                for (int i = 0; i < children.length; i++) {
                                    new File(mediaStorageDir, children[i]).delete();
                                }
                            }
                        }

                        @Override
                        public void onProgress(String message) {
                            Log.d("videoFrames", " progress getting frames from video");
                        }

                        @Override
                        public void onFailure(String message) {
                            Log.d("videoFrames", " failed to get frames from video");
                            Log.d("videoFrames", " failure reason " + message);
                            Log.d("videoFrames", " ----------------------------------------------- ");
                        }

                        @Override
                        public void onSuccess(String message) {
                            Log.d("videoFrames", " success getting frames from video");
                        }

                        @Override
                        public void onFinish() {
                            Log.d("videoFrames", " finished getting frames from video");
                        }
                    });
                } catch (FFmpegCommandAlreadyRunningException e) {
                    Log.d("videoFrames", " command already running in fmpeg");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinish() {}
        });
    } catch (FFmpegNotSupportedException e) {
        // Handle if FFmpeg is not supported by device
    }

更新3

通过执行以下操作修复了我的代码:

 final String cmd[] = {

            "-i",
            videoCroppedFile.getAbsolutePath(),
            "-f",
            "image2",
            "-ss",
            String.valueOf(videoLength.floatValue() / (8.0  * 10.0)),
            "-r",
            String.valueOf(8.0 / videoLength.floatValue()),
            mediaStorageDir.getAbsolutePath() + "/%d.jpg"
    };

0 个答案:

没有答案