bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.51k stars 1.58k forks source link

DemuxException: type = CodecUnsupported, info = Flv: Unsupported codec in video frame: 12 #1962

Open ayoullf opened 1 year ago

ayoullf commented 1 year ago

javacv version is 1.5.8,the server is nginx-http-flv-module,the front is : vue+flv.js; then the page console print error:DemuxException: type = CodecUnsupported, info = Flv: Unsupported codec in video frame: 12

my code is: `public static Boolean start(String deviceId, String formUrl, String toUrl) { if (null != get(deviceId)) return true; final RtmpAnalyseHandler convertVideoPakcet = new RtmpAnalyseHandler(); BaseConstants.CONVERT_VIDEO_PACKETS.put(deviceId, convertVideoPakcet); new Thread(() -> { LOG.info("start device"); try { convertVideoPakcet.rtsp(formUrl).rtmp(toUrl).start(); } catch (IOException e) { LOG.error("start dvice error,{}", e); } catch (Exception e) { e.printStackTrace(); } }).start(); LOG.info("start device finish!"); return true; }

/**
 *
 * @param id
 * @return
 */
public static Boolean stop(String id) {
    LOG.info("stop device ,{}", id);
    RtmpAnalyseHandler convertVideoPakcet = get(id);
    if (null != convertVideoPakcet) {
        BaseConstants.CONVERT_VIDEO_PACKETS.remove(id);
        return convertVideoPakcet.stop();
    }
    return false;
}

/**
 *
 * @param src 
 * @throws Exception
 * @author JW
 */
private RtmpAnalyseHandler rtsp(String src) throws Exception {
    grabber = new FFmpegFrameGrabber(src);
    grabber.setOption("rtsp_transport", "tcp");
    LOG.info("grabber start begin");

    grabber.start();
    LOG.info("grabber start end");

    if (width < 0 || height < 0) {
        width = grabber.getImageWidth();
        height = grabber.getImageHeight();
    }
    audiocodecid = grabber.getAudioCodec();
    codecid = grabber.getVideoCodec();
    framerate = grabber.getVideoFrameRate();
    bitrate = grabber.getVideoBitrate();
    audioChannels = grabber.getAudioChannels();
    audioBitrate = grabber.getAudioBitrate();
    if (audioBitrate < 1) {
        audioBitrate = 128 * 1000;
    }
    return this;
}

/**
 * @throws IOException
 * @author JW
 */
private RtmpAnalyseHandler rtmp(String out) throws IOException {
    LOG.info("rtmp start begin");
    record = new FFmpegFrameRecorder(out, width, height);
    record.setVideoOption("tune", "zerolatency"); 
    record.setVideoOption("preset", "ultrafast"); 
    record.setVideoOption("crf", "28"); 
    record.setMaxDelay(500000);
    record.setGopSize(10);

    record.setFrameRate(framerate);
    record.setVideoBitrate(bitrate);

// record.setAudioChannels(audioChannels); // record.setAudioBitrate(audioBitrate); // record.setSampleRate(sampleRate); AVFormatContext fc = null; record.setVideoOption("preset", "ultrafast"); record.setFormat("flv"); record.setAudioCodecName("aac"); // record.setVideoCodec(codecid); record.setVideoCodec(avcodec.AV_CODEC_ID_H264); fc = grabber.getFormatContext(); record.start(fc); // record.start(); LOG.info("rtmp start end"); return this; }

/**
 * 
 *
 * @throws IOException
 * @author eguid
 */
private void start() throws IOException {
    LOG.info("rtmp start……");
    if (null != grabber)
        grabber.flush();
    while (flag) {
        AVPacket pkt = null;
        try {
            pkt = grabber.grabPacket();
            if (pkt == null || pkt.size() <= 0 || pkt.data() == null) {
                continue;
            }
            record.recordPacket(pkt);
            avcodec.av_packet_unref(pkt);

            try {
                Thread.sleep(0, 1000);
            } catch (InterruptedException e) {
                LOG.error("error,{}", e);
            }
        } catch (Exception e) {
            LOG.error("error,{}", e);
        }
    }
}`

Does anyone know why? thank you very much!

saudet commented 1 year ago

Please try to set a codec supported by your server.

ayoullf commented 1 year ago

Hello, do you mean to add it in nginx-http-flv-module or on the Java side

saudet commented 1 year ago

If you can add support for that codec to your server that works too of course.