javacv错误(已调用setFormat()?)_

时间:2015-01-18 11:24:52

标签: opencv javacv

嗨我正在制作用javacv录制带有音频的视频的节目,但我收到了一些错误。有什么建议吗?

lib版本:jdk 1.8 javacv 0.8 opencv 2.4.9

Exception in thread "main" org.bytedeco.javacv.FrameGrabber$Exception: avformat_open_input() error -2: Could not open input "output.mp4". (Has setFormat() been called?)
    at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:362)
    at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:312)
    at com.unomic.securobot.javacv.main(javacv.java:14)

我的代码

FFmpegFrameGrabber grabber1 = new FFmpegFrameGrabber("output.mp4");
                FFmpegFrameGrabber grabber2 = new FFmpegFrameGrabber("test.mp3"); 
                grabber1.setFormat("mp4");
                grabber1.start(); 
                grabber2.start(); 
                FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("outputFinal.mp4", 
                        grabber1.getImageWidth(), grabber1.getImageHeight(), 
                        grabber2.getAudioChannels()); 
                recorder.setFrameRate(grabber1.getFrameRate()); 
                recorder.setSampleFormat(grabber2.getSampleFormat()); 
                recorder.setSampleRate(grabber2.getSampleRate()); 

                recorder.start(); 
                Frame frame1;
                Frame  frame2 = null; 
                while ((frame1 = grabber1.grabFrame()) != null || 
                       (frame2 = grabber2.grabFrame()) != null) { 
                    recorder.record(frame1); 
                    recorder.record(frame2); 
                } 
                recorder.stop(); 
                grabber1.stop(); 
                grabber2.stop(); 
}

1 个答案:

答案 0 :(得分:1)

我试图使用framegrabber从视频中获取缩略图。我得到了相同的错误,但后来我尝试给出文件的完整路径,并且它有效。以前,我使用的是不起作用的相对路径。当我给出完整的路径它开始工作。

package com.tape.controller;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.OpenCVFrameGrabber;

public class VideoThumbTaker {

    protected String ffmpegApp;

    public VideoThumbTaker(String ffmpegApp)
    {
        this.ffmpegApp = ffmpegApp;
    }

    public void getThumb(String videoFilename, String thumbFilename, int width, int height,int hour, int min, float sec)
      throws IOException, InterruptedException
    {
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y", "-i", videoFilename, "-vframes", "1",
    "-ss", hour + ":" + min + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height, "-an", thumbFilename);
        Process process = processBuilder.start();
        InputStream stderr = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null);
        process.waitFor();
    }

    public static void main(String[] args) throws Exception, IOException
    {
         //Both case work

        FFmpegFrameGrabber g = new FFmpegFrameGrabber("C:\\JavaEE\\New Project\\tape\\src\\main\\webapp\\web-resources\\videos\\vid.mp4");
        g.setFormat("mp4");
        g.start();

        for (int i = 0 ; i < 50 ; i++) {
            ImageIO.write(g.grab().getBufferedImage(), "png", new File("C:\\JavaEE\\New Project\\tape\\src\\main\\webapp\\web-resources\\thumbnails\\video-frame-" + System.currentTimeMillis() + ".png"));
        }

         g.stop();
    }
}