使用 AndroidSequenceEncoder 时接收"No frames output"



我正在尝试使用AndroidSequenceCoder创建视频幻灯片。为了做到这一点,我有一种方法可以检索位图的数组列表和代表视频文件名的字符串。以下是我方法的代码:

public class ImageToVideo
{
    //used to input frames (images) into the video file
    private AndroidSequenceEncoder ase;
    //Arraylist containing the frames to input
    private ArrayList<Bitmap> bi;
    //temporary variable used for AsyncTask below
    private Bitmap image;
    //This method is called until main code can be written to retrieve the proper length of the audio file
    public AndroidSequenceEncoder videoEncode(final ArrayList<Bitmap> images, String filename) throws  IOException
    {
        ase = AndroidSequenceEncoder.createSequenceEncoder(new File(filename), 3);
        bi = images;
        Log.d("SEQUENCEENCODER", "SequenceEncoder created");
        //outer for-loop; goes through each image individually
        for(int i = 0; i < images.size(); i++)
        {
            Log.d("SEQUENCEENCODER", "Retrieving image " + i + " of " + (images.size() - 1));
        //inner for-loop; adds the given image a number of times determined by how long each image is on-screen
        //since encoder's fps is currently set to 3, this loops once for each second the picture should be on-screen times 3
        for(int j = 0; j < 3; j++)
        {
            Log.d("SEQUENCEENCODER", "Encoding image " + i + ", " + (j+1) + " of " + 3);
            image = bi.get((i));
            //used because of error "Choreographer: Skipped ~90 frames! The application may be doing too much work on its main thread"
            AsyncTask.execute(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        ase.encodeImage(image);
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    ase.finish();
    return ase;
}

AndroidSequenceEnceNenceCoder的FPS设置为3作为临时值;当我更多地处理代码时,我也希望此方法也可以使用视频的长度,以便该方法根据每个图像在屏幕上应在屏幕上的时间确定FPS,因为它们每个图像都有相等的时间。使用4个图像运行此方法时,我会收到以下消息

D/SEQUENCEENCODER: SequenceEncoder created
D/SEQUENCEENCODER: Retrieving image 0 of 3
D/SEQUENCEENCODER: Encoding image 0, 1 of 3
D/SEQUENCEENCODER: Encoding image 0, 2 of 3
D/SEQUENCEENCODER: Encoding image 0, 3 of 3
D/SEQUENCEENCODER: Retrieving image 1 of 3
D/SEQUENCEENCODER: Encoding image 1, 1 of 3
D/SEQUENCEENCODER: Encoding image 1, 2 of 3
D/SEQUENCEENCODER: Encoding image 1, 3 of 3
D/SEQUENCEENCODER: Retrieving image 2 of 3
D/SEQUENCEENCODER: Encoding image 2, 1 of 3
D/SEQUENCEENCODER: Encoding image 2, 2 of 3
D/SEQUENCEENCODER: Encoding image 2, 3 of 3
D/SEQUENCEENCODER: Retrieving image 3 of 3
D/SEQUENCEENCODER: Encoding image 3, 1 of 3
D/SEQUENCEENCODER: Encoding image 3, 2 of 3
D/SEQUENCEENCODER: Encoding image 3, 3 of 3
I/System.out: [WARN]    . (:0): No frames output.
D/EGL_emulation: eglMakeCurrent: 0xa8a05240: ver 2 0 (tinfo 0xa8a03250)
D/EGL_emulation: eglMakeCurrent: 0xa8a05240: ver 2 0 (tinfo 0xa8a03250)
I/chatty: uid=10080(projectname) RenderThread identical 1 line
D/EGL_emulation: eglMakeCurrent: 0xa8a05240: ver 2 0 (tinfo 0xa8a03250)
I/zygote: Do partial code cache collection, code=122KB, data=94KB
I/zygote: After code cache collection, code=117KB, data=92KB
I/zygote: Increasing code cache capacity to 512KB

随后列出了一长串的系统错误,指向命令

ase.encodeImage(image);

确实在Android虚拟设备中创建了一个新的视频文件,但是播放后几乎立即结束,尽管它说其长度为4秒。我想知道我在代码中做错了什么。

我在上述代码中看到的一个问题是,帧的实际编码发生在单独的(背景)线程中。因此,在某些情况下,可能没有一个框架实际上被"完成"的时间编码。您需要做的是确保在实际发生所有框架的编码之后调用"完成"。有多种方法可以实现这一目标:

  1. 完全在异步任务中移动循环,然后完成;
  2. ,如果只有一个执行程序线程,您也可以在异步任务中执行"完成",这样您就可以依靠内部执行程序队列在最后执行您的"完成";
  3. 使用线程同步原语,例如" wait/notify"。因此,在主线程中,您可以在调用"完成"之前"等待",并且在异步任务中,您将"通知"。尽管这是一个可行的选择,但由于其他各种原因,我强烈阻止您使用它。

最新更新