JavaCV音頻推流報錯:org.bytedeco.javacv.FrameRecorder$Exception: No audio output stream (Is audioChannels > 0 and has start() been called?)


  執行推流時,給FFmpegFrameRecorder設置的參數如下(具體代碼參見Java CV本地視頻流通過幀圖片添加文本進行字幕合成 ):  

        recorder.setAudioBitrate(grabber.getAudioBitrate());
        recorder.setSampleRate(grabber.getSampleRate());
        recorder.setAudioCodec(avcodec.AV_CODEC_ID_MP3);    

 

  結果調用FFmpegFrameRecorder的record方法時拋出異常,詳見報錯日志:

准備開始推流...
Exception in thread "main" org.bytedeco.javacv.FrameRecorder$Exception: No audio output stream (Is audioChannels > 0 and has start() been called?)
    at org.bytedeco.javacv.FFmpegFrameRecorder.recordSamples(FFmpegFrameRecorder.java:1040)
    at org.bytedeco.javacv.FFmpegFrameRecorder.record(FFmpegFrameRecorder.java:922)
    at org.bytedeco.javacv.FFmpegFrameRecorder.record(FFmpegFrameRecorder.java:911)
    at com.lxy.service.SubtitleMix.main(SubtitleMix.java:77)
Input #0, matroska,webm, from 'E:\BaiduNetdiskDownload\testout.mkv':
  Metadata:
    title           : 天下足球網 http://www.txzqw.cc
    ENCODER         : Lavf58.39.101
  Duration: 00:01:11.11, start: 0.000000, bitrate: 2485 kb/s
    Stream #0:0: Video: h264 (High), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Metadata:
      ENCODER         : Lavc58.73.102 libx264
      DURATION        : 00:01:09.243000000
    Stream #0:1: Audio: vorbis, 48000 Hz, 5.1, fltp (default)
    Metadata:
      ENCODER         : Lavc58.73.102 libvorbis
      DURATION        : 00:01:09.331000000
    Stream #0:2: Subtitle: ass
    Metadata:
      ENCODER         : Lavc58.73.102 ssa
      DURATION        : 00:01:11.113000000
[libx264 @ 000000001cd89a80] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 000000001cd89a80] profile High, level 3.1, 4:2:0, 8-bit
[libx264 @ 000000001cd89a80] 264 - core 157 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'E:\BaiduNetdiskDownload\outtest.mp4':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, q=2-31, 25 fps, 12800 tbn, 25 tbc

Process finished with exit code 1

 

  按上面日志異常堆棧去跟代碼,最后定位這里:

    public boolean recordSamples(int sampleRate, int audioChannels, Buffer... samples) throws Exception {
        if (this.audio_st == null) {
            throw new Exception("No audio output stream (Is audioChannels > 0 and has start() been called?)");
        } else if (!this.started) {
            throw new Exception("start() was not called successfully!");
        }

 

  這里說audio_st是null,至於為什么是null,迷茫。Debug一看,音頻的碼率是0:

 

 

  注掉該碼率設置,推流成功。

  另外如果創建推流對象時設置音頻的聲道為0,比如這樣:

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("E:\\BaiduNetdiskDownload\\outtest.mp4",
                1280, 720);

  

  那么也會出現該異常。解決方式有兩種,一種是補設一下聲道:

recorder.setAudioChannels(1);

 

  另一種是實例化時就指定聲道:

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("E:\\BaiduNetdiskDownload\\outtest.mp4",
                1280, 720, 2);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM