void ff_mov_close_hinting(MOVTrack *track) { AVFormatContext* rtp_ctx = track->rtp_ctx; uint8_t *ptr; av_freep(&track->enc); sample_queue_free(&track->sample_queue); if (!rtp_ctx) return; if (rtp_ctx->pb) { av_write_trailer(rtp_ctx); avio_close_dyn_buf(rtp_ctx->pb, &ptr); av_free(ptr); } avformat_free_context(rtp_ctx); }
JNIEXPORT jint JNICALL Java_com_zhanghui_FFmpegAndroidCameraEncoder_MainActivity_flush (JNIEnv *env, jobject obj) { int ret; int got_frame; AVPacket enc_pkt; if (!(ofmt_ctx->streams[0]->codec->codec->capabilities & CODEC_CAP_DELAY)) return 0; while (1) { enc_pkt.data = NULL; enc_pkt.size = 0; av_init_packet(&enc_pkt); ret = avcodec_encode_video2(ofmt_ctx->streams[0]->codec, &enc_pkt, NULL, &got_frame); if (ret < 0) break; if (!got_frame){ ret = 0; break; } LOGI("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size); //Write PTS AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 }; AVRational r_framerate1 = { 60, 2 }; AVRational time_base_q = { 1, AV_TIME_BASE }; //Duration between 2 frames (us) int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1)); //内部时间戳 //Parameters enc_pkt.pts = av_rescale_q(framecnt*calc_duration, time_base_q, time_base); enc_pkt.dts = enc_pkt.pts; enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base); //转换PTS/DTS(Convert PTS/DTS) enc_pkt.pos = -1; framecnt++; ofmt_ctx->duration = enc_pkt.duration * framecnt; /* mux encoded frame */ ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt); if (ret < 0) break; } //Write file trailer av_write_trailer(ofmt_ctx); return 0; }
bool QVideoEncoder::close() { if (!m_isOpen) { return false; } // delayed frames? while (true) { AVPacket pkt = { 0 }; av_init_packet(&pkt); int got_packet = 0; int ret = avcodec_encode_video2(m_ff->codecContext, &pkt, 0, &got_packet); if (ret < 0 || !got_packet) { break; } write_frame(m_ff, &pkt); av_packet_unref(&pkt); } av_write_trailer(m_ff->formatContext); // close the codec avcodec_close(m_ff->videoStream->codec); // free the streams and other data freeFrame(); for(unsigned i = 0; i < m_ff->formatContext->nb_streams; i++) { av_freep(&m_ff->formatContext->streams[i]->codec); av_freep(&m_ff->formatContext->streams[i]); } // close the file avio_close(m_ff->formatContext->pb); // free the stream av_free(m_ff->formatContext); m_isOpen = false; return true; }
/***************************************************************************** * Close *****************************************************************************/ void CloseMux( vlc_object_t *p_this ) { sout_mux_t *p_mux = (sout_mux_t*)p_this; sout_mux_sys_t *p_sys = p_mux->p_sys; if( !p_sys->b_write_header && !p_sys->b_error && av_write_trailer( p_sys->oc ) < 0 ) { msg_Err( p_mux, "could not write trailer" ); } avformat_free_context(p_sys->oc); av_free(p_sys->io); free( p_sys->io_buffer ); free( p_sys ); }
//////////////////////////////////////////////////////////////// // ビデオキャプチャ停止 // // 引数: なし // 返値: なし //////////////////////////////////////////////////////////////// void AVI6::StopAVI( void ) { #ifndef NOAVI if(oc){ cCritical::Lock(); isAVI = false; av_write_trailer(oc); CloseStream(oc, &video_st); CloseStream(oc, &audio_st); avio_closep(&oc->pb); avformat_free_context(oc); oc = NULL; cCritical::UnLock(); } #endif }
static void write_trailer(muxer_t *muxer) { int i; muxer_priv_t *priv = muxer->priv; mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingTrailer); av_write_trailer(priv->oc); for(i = 0; i < priv->oc->nb_streams; i++) { av_freep(&(priv->oc->streams[i])); } av_freep(&priv->oc->pb); av_free(priv->oc); }
bool OutputFile::endWrap( ) { if( av_write_trailer( _formatContext ) != 0) { throw std::runtime_error( "could not write trailer" ); } avcodec_close( _stream->codec ); if( !( _formatContext->oformat->flags & AVFMT_NOFILE ) ) { avio_close( _formatContext->pb ); } avformat_free_context( _formatContext ); //freeFormat(); return true; }
void close_context(AVFormatContext **oc, AVStream **video_st){ int i; av_write_trailer(*oc); // close ts file and free memory if (*video_st) { avcodec_close((*video_st)->codec); } for(i = 0; i < (*oc)->nb_streams; i++) { av_freep((*oc)->streams[i]->codec); av_freep((*oc)->streams[i]); } avio_close((*oc)->pb); av_free(*oc); }
bool AVFormatWriter::CloseFile(void) { if (m_ctx) { av_write_trailer(m_ctx); url_fclose(m_ctx->pb); for(unsigned int i = 0; i < m_ctx->nb_streams; i++) { av_freep(&m_ctx->streams[i]); } av_free(m_ctx); m_ctx = NULL; } return true; }
EXPORT void stop_capture(void *actx) { struct liveStream *ctx = (struct liveStream *)actx; if (ctx) { dinit_filters(ctx); dinit_inputs(&ctx->inputs,&ctx->nb_input); if(ctx->oc) { av_write_trailer(ctx->oc); dinit_encoder(&ctx->oc); } av_frame_free(&ctx->OutFrame); free(ctx); } }
void record_segment_time(Output_Context *ptr_output_ctx){ if(ptr_output_ctx->start_time_mark == 0){ ptr_output_ctx->start_time_mark = 1; //printf("混蛋。。。、\n"); ptr_output_ctx->prev_segment_time = av_q2d(ptr_output_ctx->video_stream->time_base) * (ptr_output_ctx->pkt.pts ) - (double)ptr_output_ctx->ptr_format_ctx->start_time / AV_TIME_BASE; printf("ptr_output_ctx->prev_segment_time = %f \n" ,ptr_output_ctx->prev_segment_time); } ptr_output_ctx->curr_segment_time = av_q2d(ptr_output_ctx->video_stream->time_base) * (ptr_output_ctx->pkt.pts ) - (double)ptr_output_ctx->ptr_format_ctx->start_time / AV_TIME_BASE; if(ptr_output_ctx->vcodec_copy_mark == 1){ //video codecContext copy ptr_output_ctx->sync_ipts = ptr_output_ctx->curr_segment_time; } // //time meet if(ptr_output_ctx->curr_segment_time - ptr_output_ctx->prev_segment_time >= ptr_output_ctx->segment_duration){ //printf("...meet time ...\n" ); av_write_trailer(ptr_output_ctx->ptr_format_ctx); // close ts file and free memory avio_flush(ptr_output_ctx->ptr_format_ctx->pb); avio_close(ptr_output_ctx->ptr_format_ctx->pb); printf("complete the %d.ts ,and write the m3u8 file..\n" ,ptr_output_ctx->segment_no); write_m3u8_body( ptr_output_ctx ,ptr_output_ctx->curr_segment_time - ptr_output_ctx->prev_segment_time ,0 ); //concat ts file name sprintf(&(ptr_output_ctx->ts_name[ptr_output_ctx->dir_name_len]) ,"%s-%d.ts" ,ptr_output_ctx->ts_prfix_name ,++ptr_output_ctx->segment_no); if (avio_open(&(ptr_output_ctx->ptr_format_ctx->pb), ptr_output_ctx->ts_name, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "Could not open '%s'\n", ptr_output_ctx->ts_name); exit(OPEN_MUX_FILE_FAIL); } // Write a new header at the start of each file if (avformat_write_header(ptr_output_ctx->ptr_format_ctx, NULL)) { fprintf(stderr, "Could not write mpegts header to first output file\n"); exit(AVFORMAT_WRITE_HEADER_FAIL); } ptr_output_ctx->prev_segment_time = ptr_output_ctx->curr_segment_time; //place here } }
UINT CFlvUtils::CloseFlvFile() { HRESULT hr = S_OK; // Write the trailer, if any av_write_trailer(m_pAVFormatContext); if (!(m_pAVOutputFormat->flags & AVFMT_NOFILE)) { url_fclose(&m_pAVFormatContext->pb); } // Close resample context img_resample_close(m_pImgResampleContext); return hr; }
void FFmpegEncoderClose(struct FFmpegEncoder* encoder) { if (!encoder->context) { return; } av_write_trailer(encoder->context); avio_close(encoder->context->pb); if (encoder->audioCodec) { av_free(encoder->postaudioBuffer); if (encoder->audioBuffer) { av_free(encoder->audioBuffer); } #if LIBAVCODEC_VERSION_MAJOR >= 55 av_frame_free(&encoder->audioFrame); #else avcodec_free_frame(&encoder->audioFrame); #endif avcodec_close(encoder->audio); if (encoder->resampleContext) { avresample_close(encoder->resampleContext); } if (encoder->absf) { #ifdef FFMPEG_USE_NEW_BSF av_bsf_free(&encoder->absf); #else av_bitstream_filter_close(encoder->absf); encoder->absf = 0; #endif } } #if LIBAVCODEC_VERSION_MAJOR >= 55 av_frame_free(&encoder->videoFrame); #else avcodec_free_frame(&encoder->videoFrame); #endif avcodec_close(encoder->video); sws_freeContext(encoder->scaleContext); encoder->scaleContext = NULL; avformat_free_context(encoder->context); encoder->context = 0; }
bool MediaConverter::imgs2media(LPRImage *pRawImages[], size_t imgCount, EventMedia &eventMedia) { if (imgCount == 0) { printf("Input is empty.\n"); return true; } ////////////////////////////////////////////////////////////////////////// if(!initialize(pRawImages[0], "temp.avi")) { printf("Failed to initialize.\n"); return false; } ////////////////////////////////////////////////////////////////////////// if (!(mOutputFormatCtxPtr->flags & AVFMT_NOFILE)) { /*if (avio_open(&mOutputFormatCtxPtr->pb, mediaName.c_str(), AVIO_FLAG_WRITE) < 0) { printf("Could not open %s.\n", mediaName.c_str()); return false; }*/ if (avio_open_dyn_buf(&mOutputFormatCtxPtr->pb) < 0) { printf("Could not open avio buff.\n"); return false; } } ////////////////////////////////////////////////////////////////////////// // Output avformat_write_header(mOutputFormatCtxPtr, NULL); for (size_t i = 0; i < imgCount; ++ i) outputFrame(pRawImages[i]); flushFrames(); av_write_trailer(mOutputFormatCtxPtr); ////////////////////////////////////////////////////////////////////////// if (!(mOutputFormatCtxPtr->flags & AVFMT_NOFILE)) { //avio_close(mOutputFormatCtxPtr->pb); eventMedia.mBufferSize = avio_close_dyn_buf(mOutputFormatCtxPtr->pb, &eventMedia.mBufferPtr); } ////////////////////////////////////////////////////////////////////////// // 清理环境 uninitialize(); return true; }
VideoStream::~VideoStream() { /* close each codec */ if (ost) { #if ZM_FFMPEG_SVN avcodec_close(ost->codec); #else avcodec_close(&ost->codec); #endif av_free(opicture->data[0]); av_free(opicture); if (tmp_opicture) { av_free(tmp_opicture->data[0]); av_free(tmp_opicture); } av_free(video_outbuf); } /* write the trailer, if any */ av_write_trailer(ofc); /* free the streams */ for( int i = 0; i < ofc->nb_streams; i++) { av_freep(&ofc->streams[i]); } if (!(of->flags & AVFMT_NOFILE)) { /* close the output file */ #if ZM_FFMPEG_SVN #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1) avio_close(ofc->pb); #else url_fclose(ofc->pb); #endif #else url_fclose(&ofc->pb); #endif } /* free the stream */ av_free(ofc); }
status_t AVFormatWriter::Close() { TRACE("AVFormatWriter::Close\n"); if (fContext == NULL) return B_NO_INIT; if (!fHeaderWritten) return B_NOT_ALLOWED; int result = av_write_trailer(fContext); if (result < 0) TRACE(" av_write_trailer(): %d\n", result); return result == 0 ? B_OK : B_ERROR; }
void VideoWriter::endWriting() { qDebug() << "End writing"; if (currentlyWriting) { currentlyWriting = false; int out_size; while ((out_size = avcodec_encode_video(video_st->codec, video_outbuf, video_outbuf_size, NULL)) > 0) { AVPacket pkt; av_init_packet(&pkt); if (video_st->codec->coded_frame->pts != AV_NOPTS_VALUE) pkt.pts= av_rescale_q(video_st->codec->coded_frame->pts, video_st->codec->time_base, video_st->time_base); if(video_st->codec->coded_frame->key_frame) pkt.flags |= AV_PKT_FLAG_KEY; pkt.stream_index= video_st->index; pkt.data= video_outbuf; pkt.size= out_size; int ret = av_write_frame(oc, &pkt); } // write FFMPEG trailer av_write_trailer(oc); /* close each codec */ if (video_st) { avcodec_close(video_st->codec); av_free(picture->data[0]); av_free(picture); av_free(video_outbuf); video_outbuf=NULL; av_free(tmp_picture); tmp_picture=NULL; } avio_close(oc->pb); /* free the stream and context */ avformat_free_context(oc); oc=NULL; // close(info->fdts); } }
VideoStore::~VideoStore(){ /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); avcodec_close(video_st->codec); if (audio_st) avcodec_close(audio_st->codec); if (!(fmt->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(oc->pb); /* free the stream */ avformat_free_context(oc); }
/** Closes the ffmpeg file. */ asynStatus ffmpegFile::closeFile() { if (needStop == 0) { return asynError; } needStop = 0; /* write the trailer, if any. the trailer must be written * before you close the CodecContexts open when you wrote the * header; otherwise write_trailer may try to use memory that * was freed on av_codec_close() */ av_write_trailer(oc); /* close each codec */ if (video_st) { avcodec_close(video_st->codec); } /* free the streams */ for(unsigned int i = 0; i < oc->nb_streams; i++) { av_freep(&oc->streams[i]->codec); av_freep(&oc->streams[i]); } if (!(fmt->flags & AVFMT_NOFILE)) { /* close the output file */ avio_close(oc->pb); } /* free the stream */ av_free(oc); if (scArray) { scArray->release(); scArray = NULL; } if (outArray) { outArray->release(); outArray = NULL; } av_free(inPicture); av_free(scPicture); return asynSuccess; }
static bool ffmpeg_finalize(void *data) { ffmpeg_t *handle = (ffmpeg_t*)data; if (!handle) return false; deinit_thread(handle); // Flush out data still in buffers (internal, and FFmpeg internal). ffmpeg_flush_buffers(handle); deinit_thread_buf(handle); // Write final data. av_write_trailer(handle->muxer.ctx); return true; }
int dc_ffmpeg_video_muxer_close(VideoOutputFile *video_output_file) { u32 i; av_write_trailer(video_output_file->av_fmt_ctx); avio_close(video_output_file->av_fmt_ctx->pb); // free the streams for (i = 0; i < video_output_file->av_fmt_ctx->nb_streams; i++) { avcodec_close(video_output_file->av_fmt_ctx->streams[i]->codec); av_freep(&video_output_file->av_fmt_ctx->streams[i]->info); } //video_output_file->av_fmt_ctx->streams[video_output_file->vstream_idx]->codec = NULL; avformat_free_context(video_output_file->av_fmt_ctx); return 0; }
~ffmpeg_consumer() { encode_executor_.stop_execute_rest(); encode_executor_.join(); // Flush LOG_ON_ERROR2(av_interleaved_write_frame(oc_.get(), nullptr), "[ffmpeg_consumer]"); LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]"); if (!key_only_) audio_st_.reset(); video_st_.reset(); if (!(oc_->oformat->flags & AVFMT_NOFILE)) LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]"); // Close the output ffmpeg. CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; }
int H264BS2Video::close() { if ( m_outfctx != NULL ) { //printf("write trailer....\n"); av_write_trailer(m_outfctx); if (m_isfile) { //printf("close...\n"); avio_close(m_outfctx->pb); // 关闭文件 } avformat_free_context(m_outfctx); m_outfctx = NULL; avformat_close_input(&m_infctx); m_infctx = NULL; } return 0; }
void VideoEncoder::endCapture() { if(mpOutputContext) { // Flush the codex avcodec_send_frame(mpCodecContext, nullptr); flush(mpCodecContext, mpOutputContext, mpOutputStream, mFilename); av_write_trailer(mpOutputContext); avio_closep(&mpOutputContext->pb); avcodec_free_context(&mpCodecContext); av_frame_free(&mpFrame); sws_freeContext(mpSwsContext); avformat_free_context(mpOutputContext); mpOutputContext = nullptr; mpOutputStream = nullptr; } safe_delete(mpFlippedImage) }
/** \fn closeMuxer */ bool muxerFFmpeg::closeMuxer() { if(oc) { if(initialized==true) { av_write_trailer(oc); avio_close(oc->pb); } avformat_free_context(oc); oc=NULL; } for(int i=0;i<ADM_MAX_AUDIO_STREAM;i++) { audio_st[i]=NULL; } video_st=NULL; return true; }
void glw_rec_stop(glw_rec_t *gr) { int i; av_write_trailer(gr->oc); for(i = 0; i < gr->oc->nb_streams; i++) { AVStream *st = gr->oc->streams[i]; avcodec_close(st->codec); free(st->codec); free(st); } url_fclose(gr->oc->pb); free(gr->oc); free(gr->vbuf_ptr); free(gr); }
HRESULT CTMReceiverOutputPin::StopRecord() { m_bRecordStatus = FALSE; int ret = -1; Sleep(100); ret = av_write_trailer(m_fileSaverCtx); ret = avcodec_close(m_fileSaverStream->codec); for(int i=0; i<m_fileSaverCtx->nb_streams; i++) { av_freep(&(m_fileSaverCtx->streams[i]->codec)); av_freep(&(m_fileSaverCtx->streams[i])); } if(!(m_fileSaverFmt->flags & AVFMT_NOFILE)) { ret = avio_close(m_fileSaverCtx->pb); } av_free(m_fileSaverCtx); pts = 1; return S_OK; }
CVideoLivRecord::~CVideoLivRecord(void) { av_write_trailer(m_pAVFormatContext); if (m_bHasVideo){ avcodec_close(m_pVideoStream->codec); av_frame_free(&m_pVideoFrame); av_frame_free(&m_pVideoBkFrame); sws_freeContext(m_pVideoSwsctx); swr_free(&m_pVideoSwrctx); } if (m_bHasAudio){ avcodec_close(m_pAudioStream->codec); av_frame_free(&m_pVideoFrame); av_frame_free(&m_pVideoBkFrame); swr_free(&m_pAudioSwrctx); } if (!(m_pAVFormatContext->oformat->flags & AVFMT_NOFILE)) avio_close(m_pAVFormatContext->pb); avformat_free_context(m_pAVFormatContext); }
static int sff_write_packet(AVFormatContext *ctx, AVPacket *pkt) { AVIOContext *pb = NULL, *old = NULL; uint8_t *buf = NULL; int size = 0, frame_size = -1, ret = -1; static int seq = 0; ++seq; if(!ctx || !ctx->pb)return 0; if(avio_open_dyn_buf(&pb) < 0){ return -1; } pb->seekable = 0; old = ctx->pb; ctx->pb = pb; /*add more pkt info if needed, here stream_index only for illustration.*/ if(pkt){ avio_wb32(pb, pkt->stream_index); ret = av_interleaved_write_frame(ctx, pkt); }else{ avio_wb32(pb, (uint32_t)-1); ret = av_write_trailer(ctx); } size = avio_close_dyn_buf(pb, &buf); frame_size = size - 4; ctx->pb = old; if(frame_size <= 0){ //printf("write frame fail err %x %d:%d\n", ret, seq, frame_size); goto fail;/*its ok*/ } if(ret < 0){ printf("write frame fail err %x %d:%d\n", ret, seq, frame_size); goto fail; } ret = sff_write_block(ctx->pb, 2, buf, size); fail: av_freep(&buf); return ret; }
void Muxer::Free() { if(m_format_context != NULL) { // write trailer (needed to free private muxer data) if(m_started) { if(av_write_trailer(m_format_context) != 0) { // we can't throw exceptions here because this is called from the destructor Logger::LogError("[Muxer::Free] " + Logger::tr("Error: Can't write trailer, continuing anyway.", "Don't translate 'trailer'")); } m_started = false; } // destroy the encoders for(unsigned int i = 0; i < m_format_context->nb_streams; ++i) { if(m_encoders[i] != NULL) { delete m_encoders[i]; // no deadlock: nothing in Muxer is locked in this thread m_encoders[i] = NULL; } } // close file if(m_format_context->pb != NULL) { avio_close(m_format_context->pb); m_format_context->pb = NULL; } // free everything #if SSR_USE_AVFORMAT_FREE_CONTEXT avformat_free_context(m_format_context); m_format_context = NULL; #else for(unsigned int i = 0; i < m_format_context->nb_streams; ++i) { av_freep(&m_format_context->streams[i]->codec); av_freep(&m_format_context->streams[i]); } av_free(m_format_context); m_format_context = NULL; #endif } }