0
votes

I am trying to use javacv to fetch a rtsp monitoring camera and grabber the frames, but meet "avcodec_open2() error -40: Could not open audio codec" error when the grabber is prepare to start after setup the parameters.

Javacv version:

  • org.bytedeco.javacv-platform, 1.4.1
  • org.bytedeco.javacpp-presets.opencv-platform, 3.4.1-1.4.1

Here is the Jave Exception Infomation:

org.bytedeco.javacv.FrameGrabber$Exception: avcodec_open2() error -40: Could not open audio codec.  
  at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:835) ~[javacv-1.4.1.jar:1.4.1]  
  at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:663) ~[javacv-1.4.1.jar:1.4.1]  

rtsp infomation:
For the rtsp source, I also tried to download the FFplay and use command

.\ffplay.exe -rtsp_transport tcp rtsp://admin:[email protected]

to show the frames from monitoring camera, and it works, and with following information

  • Input #0, rtsp, from 'rtsp://admin:[email protected]':

    • Metadata:
      • title : Media Presentation
    • Duration: N/A, start: 0.000000, bitrate: N/A
      • Stream #0:0: Video: h264 (Main), yuvj420p(pc, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 15 fps, 24.17 tbr, 90k tbn, 30 tbc
      • Stream #0:1: Audio: aac, 16000 Hz, 1 channels, fltp
  • [aac @ 000001fc123be880] Audio object type 0 is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.

And here is the source code:

FFmpegFrameGrabber grabber = new FFmpegFrameGrabber ("rtsp://admin:[email protected]");
grabber.setImageHeight(640);
grabber.setImageWidth(360);
grabber.setOption("rtsp_transport", "tcp");
grabber.start(); // Where meet the problem

Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.convert( grabber.grab() );
ByteArrayOutputStream bateArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpeg", bateArrayOutputStream);
byte[] data = bateArrayOutputStream.toByteArray();
bateArrayOutputStream.close();

I have tested to change the rtsp to a mp4 file, the code can work.

How can I fix this problem? Thanks very much!

1
I think it's a problem with javacv to call ffmpeg. Seems the ffmpeg has a problem to deal with the acc audio stream in my rtsp resource. As I only require the image part, I have tried use grabber.setOption("an",""); and grabber.setAudioCodec(avcodec.AV_CODEC_ID_AAC); to ignore the audio part, but still meet the same issue.Jeremy Lu

1 Answers

0
votes

I add avutil.setLogCallback(FFmpegLogCallback.getInstance()); to get more logs here:

Warning: [aac @ 00000000213a2c60] Audio object type 0
Warning:  is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
Warning: [rtsp @ 00000000013fe6c0] Failed to open codec in avformat_find_stream_info

Seems FFmpeg deals with audio stream has some problems. As I do not require the audio information, finally I copy all the code of FFmpegFrameGrabber, and disable the audio code part. It can grabber the Frame successfully. But I am not sure if there is any problem by this action, and is there any better solutions?

What's more, when I use grabber.grabImage() and grabber.flush(), I feel the performance of Javacv is not very good...

I tried to grab the image once per second, but the image still delayed seriously. After run about 1 minute later, the program is still grabbing the images 40 seconds before, how can I fix this issue? Thanks very much!