diff options
Diffstat (limited to 'rotord/ofxMovieExporter.cpp')
| -rwxr-xr-x | rotord/ofxMovieExporter.cpp | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/rotord/ofxMovieExporter.cpp b/rotord/ofxMovieExporter.cpp index 815daa1..15abfe2 100755 --- a/rotord/ofxMovieExporter.cpp +++ b/rotord/ofxMovieExporter.cpp @@ -132,7 +132,8 @@ avformat_write_header(formatCtx,&options); lastFrameTime = 0; - frameNum = 0; + aframeNum = 0; + vframeNum = 0; recording = true; return true; @@ -217,7 +218,7 @@ av_write_frame(formatCtx, &pkt); } - frameNum++; + vframeNum++; } bool ofxMovieExporter::encodeFrame(unsigned char *pixels) @@ -234,46 +235,55 @@ sws_scale(convertCtx, inFrame->data, inFrame->linesize, 0, inH, outFrame->data, outFrame->linesize); + AVPacket pkt; int outSize = avcodec_encode_video(codecCtx, encodedBuf, ENCODED_FRAME_BUFFER_SIZE, outFrame); if (outSize > 0) { - AVPacket pkt; + av_init_packet(&pkt); - pkt.pts = (int64_t)frameNum*(frameInterval*(((float)videoStream->time_base.den)/videoStream->time_base.num));//ofGetFrameNum();//codecCtx->coded_frame->pts; + pkt.pts = (int64_t)vframeNum*(frameInterval*(((float)videoStream->time_base.den)/videoStream->time_base.num));//ofGetFrameNum();//codecCtx->coded_frame->pts; pkt.flags |= AV_PKT_FLAG_KEY; pkt.dts = pkt.pts; pkt.stream_index = videoStream->index; pkt.data = encodedBuf; pkt.size = outSize; av_interleaved_write_frame(formatCtx, &pkt); + + vframeNum++; } + //is it as simple as writing an audio packet for every video packet? // avcodec_encode_audio2(AVCodecContext *avctx,AVPacket *avpkt,const AVFrame *frame,int *got_packet_ptr); AVPacket apkt; av_init_packet(&apkt); - apkt.pts = (int64_t)frameNum*(frameInterval*(((float)videoStream->time_base.den)/videoStream->time_base.num));//ofGetFrameNum();//codecCtx->coded_frame->pts; - apkt.flags |= AV_PKT_FLAG_KEY; - apkt.dts = apkt.pts; - apkt.stream_index = audioStream->index; - //apkt.data = encodedBuf; - apkt.size = outSize; + apkt.pts = (int64_t)aframeNum*(aframeInterval*(((float)videoStream->time_base.den)/videoStream->time_base.num));//ofGetFrameNum();//codecCtx->coded_frame->pts; - AVFrame* afrm=avcodec_alloc_frame(); - afrm->nb_samples=44100/25; - afrm->format=AV_SAMPLE_FMT_S16; - uint8_t *d=new uint8_t[afrm->nb_samples*2*2]; - afrm->data[0]=d; + while(apkt.pts<pkt.pts) { + apkt.flags |= AV_PKT_FLAG_KEY; + apkt.dts = apkt.pts; + apkt.stream_index = audioStream->index; + //apkt.data = encodedBuf; + apkt.size = outSize; - int gpp; + AVFrame* afrm=avcodec_alloc_frame(); + afrm->nb_samples=44100/25; + afrm->format=AV_SAMPLE_FMT_S16; + uint8_t *d=new uint8_t[afrm->nb_samples*2*2]; + afrm->data[0]=d; - //avcodec_fill_audio_frame(afrm, 2, AV_SAMPLE_FMT_S16,(uint8_t *)d,(44100/25) * 2 * 2,1); + int gpp; - int audioOutSize = avcodec_encode_audio2(acodecCtx,&apkt,afrm,&gpp); + //avcodec_fill_audio_frame(afrm, 2, AV_SAMPLE_FMT_S16,(uint8_t *)d,(44100/25) * 2 * 2,1); - av_interleaved_write_frame(formatCtx, &apkt); + int audioOutSize = avcodec_encode_audio2(acodecCtx,&apkt,afrm,&gpp); + + av_interleaved_write_frame(formatCtx, &apkt); + + aframeNum++; + apkt.pts = (int64_t)aframeNum*(aframeInterval*(((float)videoStream->time_base.den)/videoStream->time_base.num));//ofGetFrameNum();//codecCtx->coded_frame->pts; + } - frameNum++; return true; } |
