Javacv: avcodec_open2() error -22 in javacv

Created on 29 Oct 2015  路  6Comments  路  Source: bytedeco/javacv

I am trying to save a video by capturing it from web camera.
I use windows 8.1 64 bit OS.
Eclipse luna and javacv, open cv and ffmpeg jars.

While executing the program I get following error:

Exception in thread "main" org.bytedeco.javacv.FrameRecorder$Exception: avcodec_open2() error -22: Could not open video codec.
at org.bytedeco.javacv.FFmpegFrameRecorder.startUnsafe(FFmpegFrameRecorder.java:529)
at org.bytedeco.javacv.FFmpegFrameRecorder.start(FFmpegFrameRecorder.java:288)
at CameraTest.main(CameraTest.java:34)

Please help me resolve this

question

Most helpful comment

Sorry, it looks like you are using Android. Please call FFmpegLogCallback.set() during init and check the Android log for more info about the error.

All 6 comments

Which device file and format are you trying to use?

Hey Saudet,

I am try to convert images to video using this code

FFmpegFrameRecorder recorder;
recorder = new FFmpegFrameRecorder(videoPath+"/" + this.FileName, 640, 480);
audioGrabber = new FFmpegFrameGrabber(this.audiopath); //At this line itself , I //get that error
imagesGrabber = new ArrayList<>();
for (int i = 0; i < myObjects.size(); i++) {
    imagesGrabber.add(new FFmpegFrameGrabber(myObjects.get(i)));
}
recorder.setFormat("mp4");
recorder.setFrameRate(audioGrabber.getFrameRate());
recorder.setSampleRate(audioGrabber.getSampleRate());
recorder.setVideoBitrate(AccessibilityEventCompat.TYPE_TOUCH_INTERACTION_START);
recorder.start(); // Error generate here <========================================== 
long startTime = System.currentTimeMillis();
Frame audioFrame = null;
audioGrabber.start();
for (int j = 0; j < imagesGrabber.size(); j++) {
    imagesGrabber.get(j).start();
    while ((audioFrame = audioGrabber.grabFrame()) != null || imagesGrabber.get(j).grabFrame() != null) {
        long ts = 3000 * (System.currentTimeMillis() - startTime);
        if (ts > recorder.getTimestamp()) {
            recorder.setTimestamp(ts);
            recorder.record(imagesGrabber.get(j).grabFrame());
            recorder.record(audioFrame);
        }
    }
    imagesGrabber.get(j).stop();
}
audioGrabber.stop();
recorder.stop();

Internaly in the FFmpegFrameRecorder.java line no 522

if((ret = avcodec.avcodec_open2(this.audio_c, this.audio_codec, var8)) < 0) {
    this.release();
    throw new Exception("avcodec_open2() error " + ret + ": Could not open audio codec.");
}

Error log
org.bytedeco.javacv.FrameRecorder$Exception: avcodec_open2() error -1: Could not open video codec.
at org.bytedeco.javacv.FFmpegFrameRecorder.startUnsafe(FFmpegFrameRecorder.java:495)
at org.bytedeco.javacv.FFmpegFrameRecorder.start(FFmpegFrameRecorder.java:268)
at com.myApp.TestActivity.makeVideo(TestActivity.java:424)
at com.myApp.TestActivity.access$300(TestActivity.java:55)
at com.myApp.TestActivity$CreateVideo.doInBackground(TestActivity.java:674)
at com.myApp.TestActivity$CreateVideo.doInBackground(TestActivity.java:638)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

Please let me know the error

Check FFmpeg's log on the console for the error message.

Hey Saudet
I have check the error log and the error is here
avcodec_open2() error -1: Could not open video codec

I am refereeing the code of @NamrataBagerwal from this link
https://github.com/bytedeco/javacv/issues/33

Sorry, it looks like you are using Android. Please call FFmpegLogCallback.set() during init and check the Android log for more info about the error.

K I will try it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bahramudin picture Bahramudin  路  3Comments

kongqw picture kongqw  路  4Comments

fif10 picture fif10  路  3Comments

iamazy picture iamazy  路  4Comments

ahmedaomda picture ahmedaomda  路  4Comments