压缩视频FFMPEG无法正常工作

时间:2015-07-30 17:42:09

标签: android video ffmpeg compression

我尝试使用FFMPEG和此库压缩视频:https://github.com/guardianproject/android-ffmpeg-java

我在项目中导入了ffmpeglib作为模块。这是用于压缩的代码:

public class MainActivity extends Activity {

    private ArrayList<Object> listVideoPaths = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getGalleryVideos();

        File videoFolderFile = new File("/storage/emulated/0/DCIM/Camera/");

        if(videoFolderFile.exists())
            Log.e("TEST FFMPEG", "video folder exist");
        else
            Log.e("TEST FFMPEG", "video folder DON'T exist");


        File videoInputFile = new File(listVideoPaths.get(0).toString());

        if(videoInputFile.exists())
            Log.e("TEST FFMPEG", "video input file exist");
        else
            Log.e("TEST FFMPEG", "video input file DON'T exist");

        File videoOutputFile = new File(videoFolderFile,"output.mp4");

        if(videoOutputFile.exists())
            Log.e("TEST FFMPEG", "video output file exist");
        else
            Log.e("TEST FFMPEG", "video output file DON'T exist");

        FfmpegController ffmpegController;

        try {
            ffmpegController = new FfmpegController(this,videoFolderFile);

            Clip mediaIn = new Clip();

            mediaIn.path = videoInputFile.getAbsolutePath();

            mediaIn.videoFps =  "25";

            ffmpegController.convertToMPEG(mediaIn, videoOutputFile.getAbsolutePath(), new ShellUtils.ShellCallback() {

                @Override
                public void shellOut(String shellLine) {
                    Log.e("TEST FFMPEG", "shellOut - " + shellLine);
                }

                @Override
                public void processComplete(int exitValue) {
                    Log.e("TEST FFMPEG", "proccess complete - " + exitValue);
                }
            });


        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            if(videoOutputFile.exists())
                Log.e("TEST FFMPEG", "finished ffmpeg ---> video output file exist");
            else
                Log.e("TEST FFMPEG", "finished ffmpeg ---> video output file DON'T exist");

        }
    }

    private void getGalleryVideos(){

        Cursor videoCursor = null;

        try {

            final String[] columns = { Media.DATA,
                    Media._ID,
                    Media.DATE_ADDED };

            final String orderBy = Media.DATE_ADDED;

            videoCursor = getContentResolver().query(
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns,
                    null, null, orderBy);

            if (videoCursor != null && videoCursor.getCount() > 0) {

                while (videoCursor.moveToNext()) {

                    int dataColumnIndex = videoCursor
                            .getColumnIndex(Media.DATA);

                    listVideoPaths.add(videoCursor
                            .getString(dataColumnIndex));

                }

            }

            Collections.sort(listVideoPaths,new Comparator());

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (videoCursor != null) {

                if (!videoCursor.isClosed()) {

                    videoCursor.close();

                }

            }

        }

    }

}

我没有错误,但视频没有播放。日志文件是:

  

3096-3096 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   视频文件夹存在07-30 14:31:57.389
  3096-3096 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   视频输入文件存在07-30 14:31:57.389
  3096-3096 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   视频输出文件DON&T; T存在于07-30 14:31:58.363中   3096-3096 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut -   /data/data/douglasanunciacao.androidffmpegjavateste/app_bin/ffmpeg -y   -i /storage/emulated/0/DCIM/Camera/VID_20150730_142330563.mp4 -f mpeg /storage/emulated/0/DCIM/Camera/output.mp4 07-30 14:31:58.385
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 警告:链接器:   /data/data/douglasanunciacao.androidffmpegjavateste/app_bin/ffmpeg   文本重定位。这浪费了内存并阻止了安全性   硬化。请修理。 07-30 14:31:58.390
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - ffmpeg版本0.11.1版权所有(c)2000-2012 FFmpeg   开发人员07-30 14:31:58.391
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 建于2014年12月22日12:52:34,gcc 4.6 20120106   (预发行)07-30 14:31:58.391
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 配置: - arch = arm --cpu = cortex-a8   --target-os = linux --enable-runtime-cpudetect --prefix = / data / data / info.guardianproject.ffmpeg / app_opt --enable-pic --disable-shared --enable-static --cross-prefix = /家庭/ n8fr8的/ dev /机器人/ NDK /工具链/臂-Linux的androideabi-4.6 /预建/ Linux的x86_64的/ bin中/臂-Linux的androideabi-   --sysroot = / home / n8fr8 / dev / android / ndk / platforms / android-16 / arch-arm --extra-cflags =&#39; -I ../ x264 -mfloat-abi = softfp -mfpu = neon -fPIE -pie&#39; --extra-ldflags =&#39; -L ../ x264 -fPIE -pie&#39; --enable-version3 --enable-gpl --disable-doc --enable-yasm --enable-decoders --enable-encoders --enable-muxers --enable-demuxers --enable-parsers --enable-protocols --enable-filters --enable-avresample --enable-libfreetype --disable-indevs --enable-indev = lavfi --disable-outdevs --enable-hwaccels --enable-ffmpeg --disable -ffplay --disable -ffprobe --disable-ffserver --disable-network --enable-libx264 --enable-zlib 07-30 14:31:58.391
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - libavutil 51. 54.100 / 51. 54.100 07-30 14:31:58.391   3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - libavcodec 54. 23.100 / 54. 23.100 07-30 14:31:58.391   3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - libavformat 54. 6.100 / 54. 6.100 07-30 14:31:58.391   3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - libavdevice 54. 0.100 / 54. 0.100 07-30 14:31:58.391   3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - libavfilter 2. 77.100 / 2. 77.100 07-30 14:31:58.391   3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - libswscale 2. 1.100 / 2. 1.100 07-30 14:31:58.391   3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - libswresample 0. 15.100 / 0. 15.100 07-30 14:31:58.391   3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - libpostproc 52. 0.100 / 52. 0.100 07-30 14:31:58.868   3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 输入#0,mov,mp4,m4a,3gp,3g2,mj2,来自   &#39; /storage/emulated/0/DCIM/Camera/VID_20150730_142330563.mp4':07-30   14:31:58.869 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - 元数据:07-30 14:31:58.869
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - major_brand:mp42 07-30 14:31:58.870
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - minor_version:0 07-30 14:31:58.871
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - compatible_brands:isommp42 07-30 14:31:58.872
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - creation_time:2015-07-30 17:23:34 07-30   14:31:58.873 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - 时长:00:00:01.89,开始时间:0.000000,   比特率:17571 kb / s 07-30 14:31:58.874
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - Stream#0:0(eng):视频:h264(高)(avc1 /   0x31637661),yuv420p,1920x1080,15874 kb / s,SAR 65536:65536 DAR 16:9,   23.90 fps,23.92 tbr,90k tbn,180k tbc 07-30 14:31:58.875 3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 元数据:07-30 14:31:58.876
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - rotate:270 07-30 14:31:58.877
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - creation_time:2015-07-30 17:23:34 07-30   14:31:58.878 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - handler_name:VideoHandle 07-30   14:31:58.878 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - Stream#0:1(eng):音频:aac(mp4a /   0x6134706D),48000 Hz,立体声,s16,127 kb / s 07-30 14:31:58.878
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 元数据:07-30 14:31:58.878
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - creation_time:2015-07-30 17:23:34 07-30   14:31:58.878 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - handler_name:SoundHandle 07-30   14:31:58.882 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - [buffer @ 0xb5cce0a0] w:1920 h:1080   pixfmt:yuv420p tb:1/90000 sar:65536/65536 sws_param:flags = 2 07-30   14:31:58.882 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - [buffersink @ 0xb5cce0d0]没有不透明的字段   提供07-30 14:31:58.891
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - [mpeg @ 0xb5c3df00] VBV缓冲区大小未设置,多路复用可能   失败07-30 14:31:58.892
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 输出#0,mpeg,to   &#39; /storage/emulated/0/DCIM/Camera/output.mp4' ;:07-30 14:31:58.894
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 元数据:07-30 14:31:58.895
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - major_brand:mp42 07-30 14:31:58.896
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - minor_version:0 07-30 14:31:58.896
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - compatible_brands:isommp42 07-30 14:31:58.897
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - creation_time:2015-07-30 17:23:34 07-30   14:31:58.898 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - 编码器:Lavf54.6.100 07-30   14:31:58.898 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - Stream#0:0(eng):视频:mpeg1video,   yuv420p,1920x1080 [SAR 65536:65536 DAR 16:9],q = 2-31,200 kb / s,90k   tbn,23.98 tbc 07-30 14:31:58.899
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 元数据:07-30 14:31:58.899
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - rotate:270 07-30 14:31:58.900
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - creation_time:2015-07-30 17:23:34 07-30   14:31:58.901 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - handler_name:VideoHandle 07-30   14:31:58.906 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - Stream#0:1(eng):音频:mp2,48000 Hz,   立体声,s16,128 kb / s 07-30 14:31:58.906
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 元数据:07-30 14:31:58.906
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - creation_time:2015-07-30 17:23:34 07-30   14:31:58.906 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - handler_name:SoundHandle 07-30   14:31:58.906 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - 流映射:07-30 14:31:58.906
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 流#0:0 - &gt; #0:0(h264 - &gt; mpeg1video)07-30   14:31:58.906 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - 流#0:1 - &gt; #0:1(aac - &gt; mp2)07-30   14:31:58.906 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - 按[q]停止,[?]寻求帮助07-30   14:31:59.824 3096-3182 / douglasanunciacao.androidffmpegjavateste   E / TESTE FFMPEG:shellOut - frame = 4 fps = 0.0 q = 2.0 size = 0kB   时间= 00:00:00.08比特率= 0.0kbits / s 07-30 14:32:02.029
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 8 fps = 2.7 q = 10.5 size = 4kB time = 00:00:00.25   比特率= 130.9kbits / s 07-30 14:32:02.536
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 13 fps = 3.7 q = 25.2 size = 696kB time = 00:00:00.45   比特率= 12427.3kbits / s 07-30 14:32:03.045
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 18 fps = 4.4 q = 31.0 size = 750kB time = 00:00:00.66   比特率= 9206.8kbits / s 07-30 14:32:03.582
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 23 fps = 5.0 q = 31.0 size = 786kB time = 00:00:00.87   比特率= 7351.4kbits / s 07-30 14:32:04.140
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 28 fps = 5.5 q = 31.0 size = 862kB time = 00:00:01.08   比特率= 6511.8kbits / s 07-30 14:32:05.239
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 30 fps = 4.8 q = 31.0 size = 876kB time = 00:00:01.16   比特率= 6144.9kbits / s 07-30 14:32:05.746
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 35 fps = 5.2 q = 31.0 size = 910kB time = 00:00:01.37   比特率= 5416.2kbits / s 07-30 14:32:06.317
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 41 fps = 5.6 q = 31.0 size = 972kB time = 00:00:01.62   比特率= 4895.2kbits / s 07-30 14:32:06.832
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - frame = 45 fps = 5.7 q = 31.0 Lsize = 1022kB   时间= 00:00:01.83比特率= 4562.1kbits / s 07-30 14:32:06.832
  3096-3182 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   shellOut - 视频:984kB音频:30kB全局标头:0kB多路复用开销   0.756932%07-30 14:32:06.858 3096-3096 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   进程完成 - 0 07-30 14:32:06.858
  3096-3096 / douglasanunciacao.androidffmpegjavateste E / TESTE FFMPEG:   terminou o ffmpeg ---&gt;视频输出文件存在

有谁知道如何解决这个问题?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我通过替换此代码成功解决了这个问题:

    Clip mediaIn = new Clip();

            mediaIn.path = videoInputFile.getAbsolutePath();

            mediaIn.videoFps = "30";

            mediaIn.videoCodec = "mpeg4";

            mediaIn.width = 640;

            mediaIn.height = 352;

            mediaIn.videoBitrate = 1000;

//            "ffmpeg -y -i /sdcard/DCIM/Camera/VID_20150728_150045662.mp4 " +
//                    "-strict experimental -s 640x352 -r 30 -vcodec mpeg4 -ab 48000 " +
//                    "-ac 2 -ar 22050 -b 1000k /sdcard/DCIM/Camera/output2.mp4";

            ffmpegController.convertToMPEG(mediaIn, videoOutputFile.getAbsolutePath(), new ShellUtils.ShellCallback() {

                @Override
                public void shellOut(String shellLine) {
                    Log.e("TEST FFMPEG", "shellOut - " + shellLine);
                }

                @Override
                public void processComplete(int exitValue) {
                    Log.e("TEST FFMPEG", "proccess complete - " + exitValue);
                }
            });

通过此代码:

Clip clipIn = new Clip(videoInputFile.getAbsolutePath());
            Clip clipOut = new Clip(videoOutputFile.getAbsolutePath());
            clipOut.videoCodec = "mpeg4";
            clipOut.videoFps = "30"; // tailor this to your needs
            clipOut.videoBitrate = 512; // 512 kbps - tailor this to your needs
            clipOut.audioChannels = 1;
            clipOut.width = 640;
            clipOut.height = 352;
            clipOut.duration = 2;
            ffmpegController.processVideo(clipIn, clipOut, true, new ShellUtils.ShellCallback() {
                @Override
                public void shellOut(String shellLine) {
                    Log.e("TEST FFMPEG", "shellOut - " + shellLine);
                }

                @Override
                public void processComplete(int exitValue) {
                    Log.e("TEST FFMPEG", "proccess complete - " + exitValue);
                }
            });