Android相机2 API Galaxy S7

6
我正在编写一个应用程序,可以从手机录制视频并将其上传到服务器。在除Galaxy S7之外的任何设备上都可以正常工作。但是在Galaxy S7上,录制的视频只有音频,没有视频或只有一帧视频。这不仅适用于上传到服务器的文件,而且适用于手机上创建的临时文件。 我正在使用Camera2 API,并尝试使用前置和后置摄像头。
我已经尝试了我的代码以及这两个示例应用程序: https://developer.android.com/samples/Camera2Video/project.html https://github.com/googlesamples/android-Camera2Video/blob/master/Application/src/main/java/com/example/android/camera2video/Camera2VideoFragment.java 生成的视频文件似乎没问题,以下是编解码器信息: 流0 类型:视频 编解码器:H264 - MPEG-4 AVC(第10部分)(avc1) 语言:英语 分辨率:960x720 显示分辨率:960x720 帧速率:29.055091

流1 类型:音频 编解码器:MPEG AAC音频(mp4a) 语言:英语 声道:立体声 采样率:16000 Hz


我进一步分析了视频文件,它报告帧率和最小帧率为0 fps,这可能是一个线索。虽然我确切地将帧率设置为30。 - user3561494
1个回答

3

经过几天的工作,我找到了答案。

三星Galaxy S7(和S6)存在一个会破坏编码的错误。 修复方法是使用下面的函数重新编码。

请注意,您需要在gradle中添加以下依赖项: compile 'com.googlecode.mp4parser:isoparser:1.1.22'

    public void fixSamsungBug()
{
    DataSource channel = null;
    try
    {
        channel = new FileDataSourceImpl(app.dataMgr.videoFileURL);
    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }

    IsoFile isoFile = null;

    try
    {
        isoFile = new IsoFile(channel);
    } catch (IOException e)
    {
        e.printStackTrace();
    }

    List<TrackBox> trackBoxes = isoFile.getMovieBox().getBoxes(TrackBox.class);
    boolean sampleError = false;
    for (TrackBox trackBox : trackBoxes) {
        TimeToSampleBox.Entry firstEntry = trackBox.getMediaBox().getMediaInformationBox().getSampleTableBox().getTimeToSampleBox().getEntries().get(0);

        // Detect if first sample is a problem and fix it in isoFile
        // This is a hack. The audio deltas are 1024 for my files, and video deltas about 3000
        // 10000 seems sufficient since for 30 fps the normal delta is about 3000
        if(firstEntry.getDelta() > 10000) {
            sampleError = true;
            firstEntry.setDelta(3000);
        }
    }

    if(sampleError) {
        Log.d("gpinterviewandroid", "Sample error! correcting...");
        Movie movie = new Movie();
        for (TrackBox trackBox : trackBoxes) {
            movie.addTrack(new Mp4TrackImpl(channel.toString() + "[" + trackBox.getTrackHeaderBox().getTrackId() + "]" , trackBox));
        }
        movie.setMatrix(isoFile.getMovieBox().getMovieHeaderBox().getMatrix());
        Container out = new DefaultMp4Builder().build(movie);

        //delete file first!
        File file = new File(app.dataMgr.videoFileURL);
        boolean deleted = file.delete();


        FileChannel fc = null;
        try
        {
            //fc = new FileOutputStream(new File(app.dataMgr.videoFileURL)).getChannel();
            fc = new RandomAccessFile(app.dataMgr.videoFileURL, "rw").getChannel();
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }

        try
        {
            out.writeContainer(fc);
            fc.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        Log.d("gpinterviewandroid", "Finished correcting raw video");
    }
}

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