ffmpeg视频录制
錄制視頻的基本步驟是:
1. 初始化ffmpeg的基本對象,并將這些對象關聯起來,然后打開文件并寫入文件頭。
2.?編碼視頻,并將編碼后數據存寫到文件中。
3.?寫入文件尾,并清理ffmpeg對象。
首先,需要初始化ffmpeg的一些對象,初始化的順序為:
創建并初始化AVOutputFormat,?基于AVOutputFormat創建并初始化AVFormatContext。
然后查找AVCodec,?基于找到的AVCodec創建并初始化AVCodecContext,打開AVCodec。
然后基于找到的AVCodec創建AVStream。
然后創建并初始化AVIOContext。
其中AVStream, AVCodec, AVCodecContext可能會有兩組,一組用來錄制音頻,一組用來錄制視頻,如下:
AVOutputFormat和AVFormatContext可以通過avformat_alloc_output_context函數來初始化。
AVCodec通過avcodec_find_encoder函數來查找
AVCodecContext通過avcodec_alloc_context3來分配
AVCodecContext初始化完成后,可以通過avcodec_open2打開編碼器
AVStream通過avformat_new_stream來分配
以上對象初始化完成后,需要將codec的信息拷貝到AVFormatContext對象中,以便與將編碼器信息存儲到文件中,這個操作可以通過avcodec_parameters_from_context操作
最后通過avio_open打開文件并初始化AVIOContext。
最后通過avformat_write_header寫入文件頭,整個初始化階段就算是完成了
以下初始化代碼供參考:
avformat_alloc_output_context2(&format_context_, nullptr, nullptr, file_path.c_str());if(format_context_ == nullptr){avformat_alloc_output_context2(&format_context_, nullptr, "mpeg", file_path.c_str());}if(format_context_ == nullptr){return false;}AVOutputFormat *output_format = format_context_->oformat;output_format->video_codec = AV_CODEC_ID_H264;AVCodec *codec = avcodec_find_encoder(output_format->video_codec);codec_context_ = avcodec_alloc_context3(codec);codec_context_->codec_id = output_format->video_codec;codec_context_->pix_fmt = AV_PIX_FMT_YUV420P;codec_context_->width = width;codec_context_->height = height;codec_context_->time_base = {1, 1000};codec_context_->gop_size = 12;if (codec_context_->codec_id == AV_CODEC_ID_MPEG2VIDEO){codec_context_->max_b_frames = 2;}if (codec_context_->codec_id == AV_CODEC_ID_MPEG1VIDEO){codec_context_->mb_decision = 2;}if (output_format->flags & AVFMT_GLOBALHEADER)codec_context_->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;int ret = avcodec_open2(codec_context_, codec, nullptr);if(ret != 0){return false;}video_stream_ = avformat_new_stream(format_context_, codec);if(video_stream_ == nullptr){return false;}ret = avcodec_parameters_from_context(video_stream_->codecpar, codec_context_);if(ret != 0){return false;}ret = avio_open(&format_context_->pb, file_path.c_str(), AVIO_FLAG_WRITE);if(ret != 0){return false;}ret = avformat_write_header(format_context_, nullptr);if(ret != 0){return false;}?注意,有些編碼器只支持一些固定的幀率,對于這樣的編碼器,AVCodecContext中的time_base是不能隨便設置的,當寫文件頭失敗時,可以檢查一下這一點
初始化完成后,就可以進行視頻編碼錄制了,跟初始化相比,編碼錄制的過程要簡單的多,核心函數就三個:
avcodec_send_frame進行視頻編碼
avcodec_receive_packet用于獲取編碼后的數據
av_write_frame用于將編碼后的數據寫入文件
以下代碼供參考:
av_image_fill_arrays(src_frame_->data, src_frame_->linesize, data,AV_PIX_FMT_RGB24, src_frame_->width, src_frame_->height, 1);sws_scale(sws_context_, src_frame_->data, src_frame_->linesize, 0, src_frame_->height,dst_frame_->data, dst_frame_->linesize);auto now_time = std::chrono::steady_clock::now();dst_frame_->pts = std::chrono::duration_cast<std::chrono::milliseconds>(now_time - start_time_point_).count();int ret = avcodec_send_frame(codec_context_, dst_frame_);if(ret == 0){AVPacket packet;av_init_packet(&packet);ret = avcodec_receive_packet(codec_context_, &packet);if(ret == 0){av_packet_rescale_ts(&packet, codec_context_->time_base, video_stream_->time_base);av_write_frame(format_context_, &packet);}av_packet_unref(&packet);}這里的第16行注意一下,將編碼后的數據寫入文件之前,一定要進行時間轉換,否則播放視頻時會出現視頻播放速度太快的問題
最后就是結束錄制了,這個過程就不用多說了,看代碼:
if(format_context_ != nullptr){av_write_trailer(format_context_);}if(sws_context_ != nullptr){sws_freeContext(sws_context_);sws_context_ = nullptr;}if(codec_context_ != nullptr){avcodec_close(codec_context_);avcodec_free_context(&codec_context_);}if(format_context_ != nullptr){avio_close(format_context_->pb);avformat_free_context(format_context_);format_context_ = nullptr;}總結
以上是生活随笔為你收集整理的ffmpeg视频录制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Microsoft-Yahoo: Not
- 下一篇: chrome(谷歌浏览器)固定到任务栏,