bytedeco/javacv

FFmpegFrameRecorder: avcodec_encode_audio2() error -12: Could not encode audio packet

Open

#1027 aperta il 9 lug 2018

Vedi su GitHub
 (15 commenti) (0 reazioni) (0 assegnatari)Java (1583 fork)batch import
enhancementhelp wanted

Metriche repository

Star
 (6985 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

Hello I'm having a problem trying to use AV_CODEC_ID_VORBIS Audio Codec as part of the code below:

            try {                  
               
                grabber.setImageWidth(CAPTUREWIDTH);
                grabber.setImageHeight(CAPTUREHRIGHT);                      
                grabber.start();
                
                recorder = new FFmpegFrameRecorder("C:\\temp\\output.webm", CAPTUREWIDTH, CAPTUREHRIGHT, 2);                  
                recorder.setInterleaved(true);                    
                
                recorder.setVideoOption("preset", "ultrafast");
                recorder.setVideoOption("crf", "28");
                recorder.setVideoBitrate(2000000);
                recorder.setVideoCodec(avcodec.AV_CODEC_ID_VP8);
                recorder.setFormat("webm");
                recorder.setFrameRate(FRAME_RATE);                    
                recorder.setGopSize(GOP_LENGTH_IN_FRAMES);                    
                
                recorder.setAudioOption("crf", "0");
                recorder.setAudioQuality(0);                    
                recorder.setAudioBitrate(192000);
                recorder.setSampleRate(44100);
                recorder.setAudioChannels(2);
                recorder.setAudioCodec(avcodec.AV_CODEC_ID_VORBIS);
                
                recorder.start();
                     
                AudioFormat audioFormat = new AudioFormat(44100, 16, 2, true, true);                    
                DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat); 
                try 
                {                        
                    final TargetDataLine line = (TargetDataLine)AudioSystem.getLine(dataLineInfo); 
                    line.open(audioFormat); 
                    line.start(); 

                    final int sampleRate = (int) audioFormat.getSampleRate(); 
                    final int numChannels = audioFormat.getChannels(); 
                   
                    int audioBufferSize = sampleRate * numChannels; 
                    final byte[] audioBytes = new byte[audioBufferSize]; 
                   
                    ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1); 
                    exec.scheduleAtFixedRate(new Runnable() { 
                        @Override 
                        public void run() 
                        { 
                            try 
                            { 
                                int nBytesRead = line.read(audioBytes, 0, line.available()); 

                                int nSamplesRead = nBytesRead / 2 ; 
                                short[] samples = new short[nSamplesRead]; 
                                ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(samples); 
                                ShortBuffer sBuff = ShortBuffer.wrap(samples, 0, nSamplesRead); 

                                //TODO at this point the error occurs
                                recorder.recordSamples(sampleRate, numChannels, sBuff); 
                            }  
                            catch (org.bytedeco.javacv.FrameRecorder.Exception e) 
                            { 
                                e.printStackTrace(); 
                            } 
                        } 
                    }, 0, (long) 1000 / FRAME_RATE, TimeUnit.MILLISECONDS); 
                }  
                catch (LineUnavailableException e1) 
                { 
                    e1.printStackTrace(); 
                } 
               
                frame = 0;
                Frame capturedFrame;
                long startTime = System.currentTimeMillis();
                
                while ((capturedFrame = grabber.grab()) != null && runnable) {
                    
                    BufferedImage buff = paintConverter.getBufferedImage(capturedFrame, 1);
                    
                    Graphics g = canvas.getGraphics();
                    g.drawImage(buff, 0, 0, CAPTUREWIDTH, CAPTUREHRIGHT, 0, 0, buff.getWidth(), buff.getHeight(), null);
                    
                    recorder.record(capturedFrame);
                    frame++;
                    
                    long waitMillis = 1000 * frame / FRAME_RATE - (System.currentTimeMillis() - startTime);
                    while (waitMillis <= 0) {
                        recorder.record(capturedFrame);
                        frame++;
                        waitMillis = 1000 * frame / FRAME_RATE - (System.currentTimeMillis() - startTime);
                    }
                    Thread.sleep(waitMillis);                        
                } //fim while recorder
            } catch (FrameGrabber.Exception ex) {
                Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FrameRecorder.Exception ex) {
                Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
            }
     

on the line : //TODO at this point the error occurs recorder.recordSamples(sampleRate, numChannels, sBuff);

when trying to record the audio occurs the problem:

org.bytedeco.javacv.FrameRecorder$Exception: avcodec_encode_audio2() error -12: Could not encode audio packet.
	at org.bytedeco.javacv.FFmpegFrameRecorder.record(FFmpegFrameRecorder.java:1142)
	at org.bytedeco.javacv.FFmpegFrameRecorder.writeSamples(FFmpegFrameRecorder.java:1132)
	at org.bytedeco.javacv.FFmpegFrameRecorder.recordSamples(FFmpegFrameRecorder.java:1107)

someone has already faced this problem using the Vorbis codec with VP8?

removing this line of code the video is recorded without the audio as output below:

[libvpx @ 000000001d70ec80] v1.7.0
Output #0, webm, to 'C:\temp\output.webm':
  Metadata:
    encoder         : Lavf58.12.100
    Stream #0:0: Video: vp8, yuv420p, 320x180, q=2-31, 2000 kb/s, 1k tbn
    Stream #0:1: Audio: vorbis, 44100 Hz, stereo, fltp
Input #0, matroska,webm, from 'C:\temp\output.webm':
  Metadata:
    ENCODER         : Lavf58.12.100
  Duration: 00:00:03.12, start: 0.000000, bitrate: 258 kb/s
    Stream #0:0: Video: vp8, yuv420p(progressive), 320x180, SAR 1:1 DAR 16:9, 1k fps, 25 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      DURATION        : 00:00:03.120000000
    Stream #0:1: Audio: vorbis, 44100 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:00:00.000000000

I am using the version javacpp-presets/1.4.2-SNAPSHOT

Guida contributor