Android中的JavaCV录制视频

时间:2017-01-11 17:08:02

标签: ffmpeg javacv

我想在Android中录制视频安静而无需预览。所以我选择了 MediaRecorder ,但我只能在没有预览的情况下进行录制,但让我发疯的是,当MediaRecorder开始或停止时,它会以声音结束...... 。我尝试了很多关于that的方法。但我认为它可能与移动设备的操作系统有关。所以我尝试使用JavaCV,因为我也想在我的应用程序中使用Live功能。

JavaCV花了我太多时间来解决一些奇怪的问题,因为这是我第一次做C ++ src和视频。

只有compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.3'作为README.md,我甚至无法构建我的apk。

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK org/bytedeco/javacpp/macosx-x86_64/libusb-1.0.dylib
    File1: /Users/wyx/.gradle/caches/modules-2/files-2.1/org.bytedeco.javacpp-presets/libfreenect/0.5.3-1.3/736d65a3ef042258429d8e7742128c411806b432/libfreenect-0.5.3-1.3-macosx-x86_64.jar
    File2: /Users/wyx/.gradle/caches/modules-2/files-2.1/org.bytedeco.javacpp-presets/libdc1394/2.2.4-1.3/f1498dacc46162ab68faeb8d66cf02b96fe41c61/libdc1394-2.2.4-1.3-macosx-x86_64.jar

然后我根据issuse修改了它 用它来重新定位。它可以构建apk。但是无法运行。

  android {
   ..............
    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        pickFirst  'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.properties'
        pickFirst  'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.xml'
        pickFirst  'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.properties'
        pickFirst  'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.xml'
    }
}

dependencies {
    compile group: 'org.bytedeco', name: 'javacv', version: '1.3'
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '3.2.1-1.3', classifier: 'android-x86'
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '3.2.1-1.3', classifier: 'android-arm'
    compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.1.0-1.3', classifier: 'android-x86'
    compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.1.0-1.3', classifier: 'android-arm'
}

我的演示代码 VideoService 将在MainActivity中调用

package com.fs.fs.api;

import com.fs.fs.App;
import com.fs.fs.utils.DateUtils;
import com.fs.fs.utils.FileUtils;

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameRecorder;

import java.util.Date;

/**
 * Created by wyx on 2017/1/11.
 */
public class VideoService {
    private FFmpegFrameRecorder mFrameRecorder;
    private String path;

    private VideoService() {
    }

    private static class SingletonHolder {
        private static final VideoService INSTANCE = new VideoService();
    }

    public static VideoService getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void startRecordVideo() {
        String fileName = String.format("%s.%s", DateUtils.date2String(new Date(), "yyyyMMdd_HHmmss"), "mp4");
        path = FileUtils.getExternalFullPath(App.getInstance(), fileName);
        mFrameRecorder = new FFmpegFrameRecorder(path, 640, 480, 1);
        mFrameRecorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        mFrameRecorder.setVideoOption("tune", "zerolatency");
        mFrameRecorder.setVideoOption("preset", "ultrafast");
        mFrameRecorder.setVideoOption("crf", "28");
        mFrameRecorder.setVideoBitrate(300 * 1000);
        mFrameRecorder.setFormat("mp4");

        mFrameRecorder.setFrameRate(30);
        mFrameRecorder.setAudioOption("crf", "0");
        mFrameRecorder.setSampleRate(48 * 1000);
        mFrameRecorder.setAudioBitrate(960 * 1000);
        mFrameRecorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
        try {
            mFrameRecorder.start();
        } catch (FrameRecorder.Exception e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        if (mFrameRecorder != null) {
            try {
                mFrameRecorder.stop();
                mFrameRecorder.release();
            } catch (FrameRecorder.Exception e) {
                e.printStackTrace();
            }
            mFrameRecorder = null;
        }
    }

}

MainActivity

package com.fs.fs.activity;

import android.app.Activity;
import android.os.Bundle;

import com.fs.fs.R;
import com.fs.fs.api.VideoService;

import static java.lang.Thread.sleep;


public class MainActivity extends Activity {

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


        VideoService.getInstance().startRecordVideo();
        try {
            sleep(10 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        VideoService.getInstance().stop();
    }
}

让我想哭的错误。

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.fs.fs, PID: 30259
                  java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: org.bytedeco.javacpp.avutil
                      at org.bytedeco.javacpp.Loader.load(Loader.java:590)
                      at org.bytedeco.javacpp.Loader.load(Loader.java:530)
                      at org.bytedeco.javacpp.avcodec$AVPacket.<clinit>(avcodec.java:1694)
                      at org.bytedeco.javacv.FFmpegFrameRecorder.<init>(FFmpegFrameRecorder.java:149)
                      at com.fs.fs.api.VideoService.startRecordVideo(VideoService.java:34)
                      at com.fs.fs.activity.MainActivity.onCreate(MainActivity.java:75)
                      at android.app.Activity.performCreate(Activity.java:5304)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2331)
                      at android.app.ActivityThread.access$1000(ActivityThread.java:143)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5291)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
                      at dalvik.system.NativeStart.main(Native Method)
                   Caused by: java.lang.ClassNotFoundException: org.bytedeco.javacpp.avutil
                      at java.lang.Class.classForName(Native Method)
                      at java.lang.Class.forName(Class.java:251)
                      at org.bytedeco.javacpp.Loader.load(Loader.java:585)
                      at org.bytedeco.javacpp.Loader.load(Loader.java:530) 
                      at org.bytedeco.javacpp.avcodec$AVPacket.<clinit>(avcodec.java:1694) 
                      at org.bytedeco.javacv.FFmpegFrameRecorder.<init>(FFmpegFrameRecorder.java:149) 
                      at com.fs.fs.api.VideoService.startRecordVideo(VideoService.java:34) 
                      at com.fs.fs.activity.MainActivity.onCreate(MainActivity.java:75) 
                      at android.app.Activity.performCreate(Activity.java:5304) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2331) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:143) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:136) 
                      at android.app.ActivityThread.main(ActivityThread.java:5291) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
                      at dalvik.system.NativeStart.main(Native Method) 
                   Caused by: java.lang.NoClassDefFoundError: org/bytedeco/javacpp/avutil
                      at java.lang.Class.classForName(Native Method) 
                      at java.lang.Class.forName(Class.java:251) 
                      at org.bytedeco.javacpp.Loader.load(Loader.java:585) 
                      at org.bytedeco.javacpp.Loader.load(Loader.java:530) 
                      at org.bytedeco.javacpp.avcodec$AVPacket.<clinit>(avcodec.java:1694) 
                      at org.bytedeco.javacv.FFmpegFrameRecorder.<init>(FFmpegFrameRecorder.java:149) 
                      at com.fs.fs.api.VideoService.startRecordVideo(VideoService.java:34) 
                      at com.fs.fs.activity.MainActivity.onCreate(MainActivity.java:75) 
                      at android.app.Activity.performCreate(Activity.java:5304) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2331) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:143) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:136) 
                      at android.app.ActivityThread.main(ActivityThread.java:5291) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
                      at dalvik.system.NativeStart.main(Native Method) 
                   Caused by: java.lang.ClassNotFoundException: Didn't find class "org.bytedeco.javacpp.avutil" on path: DexPathList[[zip file "/data/app/com.fs.fs-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.fs.fs-2, /vendor/lib, /system/lib, /data/datalib]]
                      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                      at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
                      at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
                      at java.lang.Class.classForName(Native Method) 
                      at java.lang.Class.forName(Class.java:251) 
                      at org.bytedeco.javacpp.Loader.load(Loader.java:585) 
                      at org.bytedeco.javacpp.Loader.load(Loader.java:530) 
                      at org.bytedeco.javacpp.avcodec$AVPacket.<clinit>(avcodec.java:1694) 
                      at org.bytedeco.javacv.FFmpegFrameRecorder.<init>(FFmpegFrameRecorder.java:149) 
                      at com.fs.fs.api.VideoService.startRecordVideo(VideoService.java:34) 
                      at com.fs.fs.activity.MainActivity.onCreate(MainActivity.java:75) 
                      at android.app.Activity.performCreate(Activity.java:5304) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2331) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:143) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:136) 
                      at android.app.ActivityThread.main(ActivityThread.java:5291) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
                      at dalvik.system.NativeStart.main(Native Method) 

所以我想知道一种实现目标的舒适方法:重新编码视频,无需预览。实时实时

我发现 ffmpeg4android 是一个运行ffmpeg命令的完美库。我只是用它来压缩MediaRecorder中的视频但我不知道怎么用它来实现我的目标。

0 个答案:

没有答案