使用FFMPEg修剪视频

时间:2018-12-13 07:33:04

标签: android android-ffmpeg

在我的应用程序中,我具有修剪视频的功能。

我在这里使用FFmpeg。 下面是我的代码

在顶部声明,

 FFmpeg ffmpeg;

使用以下代码加载库

try {

        if (ffmpeg == null) {

            Log.e("loadFFMpegBinary","null");
            ffmpeg = FFmpeg.getInstance(Constants.appContext);

        }

        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {



            @Override public void onFailure() {

                Log.e("loadBinary","Failure");

            }



            @Override public void onSuccess() {


                Log.e("onSuccess","onSuccess");

            }

        });

    } catch (FFmpegNotSupportedException e) {
        Log.e("FFmpegNot",":-"+e);


    } 

使用以下代码进行修整

File externalStoragePublicDirectory = new File(destPath);
    if (!externalStoragePublicDirectory.exists() ? externalStoragePublicDirectory.mkdir() : true) {

        String filePrefix = yourRealPath.substring(yourRealPath.lastIndexOf("."));
        String destFileName = getString(R.string.app_name) + System.currentTimeMillis();
        boolean isFastMode = false;
        File dest = (filePrefix.equals(".webm") || filePrefix.equals(".mkv")) ? new File(externalStoragePublicDirectory, destFileName + ".mp4") : new File(externalStoragePublicDirectory, destFileName + filePrefix);
        int fileNo = 0;
        while (dest.exists()) {
            fileNo++;
            dest = (filePrefix.equals(".webm") || filePrefix.equals(".mkv")) ? new File(externalStoragePublicDirectory, destFileName + fileNo + ".mp4") : new File(externalStoragePublicDirectory, destFileName + fileNo + filePrefix);
        }
        Log.e(TAG, "startTrim: src: " + yourRealPath);
        Log.e(TAG, "startTrim: dest: " + dest.getAbsolutePath());
        Log.e(TAG, "startTrim: startMs: " + startMs);
        Log.e(TAG, "startTrim: endMs: " + endMs);
        String filePath = dest.getAbsolutePath();

        Log.e(TAG, "startTrim: filePath: " + filePath);


        String[] complexCommand = {"-ss", "" + startMs / 1000, "-y", "-i",
                yourRealPath, "-t", "" + (endMs - startMs) / 1000, "-s",
                "320x240", "-r", "15", "-vcodec", "mpeg4", "-b:v", "2097152",
                "-b:a", "48000", "-ac", "2", "-ar", "22050", dest.getAbsolutePath()};

try {

        ffmpeg.execute(complexCommand, new ExecuteBinaryResponseHandler() {
            @Override
            public void onFailure(String s) {
                Log.e(TAG, "FAILED with output : " + s);
            }

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

            }

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

            }

            @Override
            public void onStart() {
                Log.e(TAG, "Started command : ffmpeg " + command);

            }

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


            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        Log.e("Exception",";-"+e);
    }

这里我遇到以下错误,

  

12-13 12:53:38.057 31029-31029 / com.jobsounder E / VideoCropperFragment:FAILED with output:ffmpeg version n3.0.1版权所有(c)2000-2016 FFmpeg开发人员         内置gcc 4.8(GCC)         配置:--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-严格溢出-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       输入#0,mov,mp4,m4a,3gp,3g2,mj2,来自“ /storage/emulated/0/Download/VID-20181211-WA0008.mp4”:         元数据:           major_brand:isom           minor_version:512           兼容品牌:isomiso2avc1mp41           编码器:Lavf56.40.101           评论:vid:v0702afc0000beoc8l4hfpit57rr9vbg         持续时间:00:00:15.09,开始:0.000000,比特率:1550 kb / s           流#0:0(und):视频:h264(高)(avc1 / 0x31637661),yuv420p,576x1024、1478 kb / s,27.43 fps,27.43 tbr,409k tbn,54.87 tbc(默认)           元数据:             handler_name:VideoHandler           流#0:1(und):音频:aac(LC)(mp4a / 0x6134706D),44100 Hz,立体声,fltp,64 kb / s(默认)           元数据:             handler_name:SoundHandler       /data/app/com.jobsounder-2/lib/arm/JobSounder1544685817737.mp4:权限被拒绝   12-13 12:53:38.057 31029-31029 / com.jobsounder E / VideoCropperFragment:完成的命令:ffmpeg [Ljava.lang.String; @ 11c7227b

0 个答案:

没有答案