shelarbapurao / javacv

Automatically exported from code.google.com/p/javacv
GNU General Public License v2.0
0 stars 0 forks source link

Get duration base on the audio track #328

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Right now there is no way to get the duration base on the audio track for 
FFmpegFrameRecorder since it is based on the frame number. It would be nice to 
include a way to get it base on the samples as well. 

The main reason is that audio sometimes need to be recorded separately on 
Android since onPreviewFrame may require a buffer to process the images and it 
is much slower than audio recording. At the same time GC may delay the audio 
thread and cause async issues. :( 

Thanks for all the hard work Samuel.

Original issue reported on code.google.com by wzsddtc on 14 Jun 2013 at 4:37

GoogleCodeExporter commented 9 years ago
It seems like it should work if the imageWidth and imageHeight is 0 so that it 
would call super.getFrameNumber(), but it is always returned 0 since it was 
never incremented in super. 

Original comment by wzsddtc on 14 Jun 2013 at 4:51

GoogleCodeExporter commented 9 years ago
(also note that audio_c.frame_number() is always much bigger than 
picture.pts().. not sure which one to use. )

Original comment by wzsddtc on 14 Jun 2013 at 5:21

GoogleCodeExporter commented 9 years ago
The following change works. :) 

private int audio_clock; 

    @Override
    public long getTimestamp() {
        if (picture == null) {
            return (audio_clock * 1000000L) / sampleRate;
        } else {
            return Math.round(getFrameNumber() * 1000000L / getFrameRate());
        }
    }

    boolean record(AVFrame frame) throws Exception {
        int ret;

        av_init_packet(audio_pkt);
        audio_pkt.data(audio_outbuf);
        audio_pkt.size(audio_outbuf_size);
        if ((ret = avcodec_encode_audio2(audio_c, audio_pkt, frame, got_audio_packet)) < 0) {
            throw new Exception("avcodec_encode_audio2() error " + ret + ": Could not encode audio packet.");
        } else {
            if(frame != null){
                this.audio_clock += frame.nb_samples();
            }
        }

Original comment by wzsddtc on 14 Jun 2013 at 6:16

GoogleCodeExporter commented 9 years ago
Hi wzsddtc,

I have same problem that images and it is much slower than audio recording, So 
would you please advice me to fix this problem, maybe some sample code(I'm very 
newbie in development).

Thanks so much!

Original comment by geuma...@gmail.com on 19 Jun 2013 at 11:10

GoogleCodeExporter commented 9 years ago
Yes, you are right, forgot about timestamps of audio frame. Should be fixed 
with this update:
http://code.google.com/p/javacv/source/detail?r=a38bec82311333d7ae7d06decdd4a2af
e7c92455
Thanks!

Original comment by samuel.a...@gmail.com on 22 Jun 2013 at 3:59

GoogleCodeExporter commented 9 years ago
Thanks for the update. This should be added to FFmpegFrameRecorder as well, do 
you know  how to set a timestamp on the audio track (to skip audio samples) in 
addition to the frame number?

Original comment by wzsddtc on 22 Jun 2013 at 5:46

GoogleCodeExporter commented 9 years ago
Are you really talking about FFmpegFrameRecorder? I'm not sure I understand the 
use case then. I'm also not sure about the technically feasible of what you are 
asking. Do you have any reference about what it is exactly you are looking to 
accomplish? A message thread from the FFmpeg mailing list perhaps? Thanks!

Original comment by samuel.a...@gmail.com on 23 Jun 2013 at 12:00

GoogleCodeExporter commented 9 years ago
This case should be closed. Audio timestamps can be grabbed by calculating how 
many samples are calculated already in FFmpegFrameRecorder OR how many samples 
are grabbed in FFmpegFrameGrabber. :) 

Original comment by wzsddtc on 16 Sep 2013 at 5:06

GoogleCodeExporter commented 9 years ago
Ok, sounds good :) I was under the impression that you were trying to skip 
audio frames on record(). This makes sense for video (we get variable frame 
rate), but I wasn't sure how that would make sense for audio...

Original comment by samuel.a...@gmail.com on 16 Sep 2013 at 5:24