1. 程式人生 > 其它 >(轉載)FFMpeg取一張圖片

(轉載)FFMpeg取一張圖片

int writeJPEG(AVFrame* frame, int width, int height){
    const char* out_file = "hello_world.jpg";
    //新建一個輸出的AVFormatContext 並分配記憶體
    AVFormatContext* output_cxt = avformat_alloc_context();
    avformat_alloc_output_context2(&output_cxt, NULL, "singlejpeg", out_file);

    //設定輸出檔案的格式
    // output_cxt->oformat = av_guess_format("mjpeg",NULL,NULL);

    
//建立和初始化一個和該URL相關的AVIOContext if (avio_open(&output_cxt->pb, out_file, AVIO_FLAG_READ_WRITE) < 0){ av_log(NULL, AV_LOG_ERROR, "不能開啟檔案 \n"); return -1; } //構建新的Stream AVStream* stream = avformat_new_stream(output_cxt, NULL); if (stream == NULL){ av_log(NULL, AV_LOG_ERROR,
"建立AVStream失敗 \n"); return -1; } //初始化AVStream資訊 AVCodecContext* codec_cxt = stream->codec; codec_cxt->codec_id = output_cxt->oformat->video_codec; codec_cxt->codec_type = AVMEDIA_TYPE_VIDEO; codec_cxt->pix_fmt = AV_PIX_FMT_YUVJ420P; codec_cxt->height = height; codec_cxt
->width = width; codec_cxt->time_base.num = 1; codec_cxt->time_base.den = 25; //列印輸出檔案資訊 av_dump_format(output_cxt, 0, out_file, 1); AVCodec* codec = avcodec_find_encoder(codec_cxt->codec_id); if (!codec){ av_log(NULL, AV_LOG_ERROR, "找不到編碼器 \n"); return -1; } if (avcodec_open2(codec_cxt, codec, NULL) < 0){ av_log(NULL, AV_LOG_ERROR, "不能開啟編碼器 \n"); return -1; } avcodec_parameters_from_context(stream->codecpar, codec_cxt); //寫入檔案頭 avformat_write_header(output_cxt, NULL); int size = codec_cxt->width * codec_cxt->height; AVPacket* packet; //av_new_packet(packet, size * 3); av_new_packet(packet, size); int got_picture = 0; int result = avcodec_encode_video2(codec_cxt, packet, frame, &got_picture); if (result < 0){ av_log(NULL, AV_LOG_ERROR, "編碼失敗 \n"); return -1; } printf("got_picture %d \n", got_picture); if (got_picture == 1){ //將packet中的資料寫入本地檔案 result = av_write_frame(output_cxt, packet); } av_free_packet(packet); //將流尾寫入輸出媒體檔案並釋放檔案資料 av_write_trailer(output_cxt); if (frame){ av_frame_unref(frame); } avio_close(output_cxt->pb); avformat_free_context(output_cxt); return 0; } void FFmpegDemo::on_pictureBtn1_clicked() { // char* filename = "./jichi.mp4"; //如果本地沒有視訊可以使用這個直播流地址 char* filename = "./2.mp4"; AVFormatContext* avf_cxt = avformat_alloc_context(); int ret = avformat_open_input(&avf_cxt, filename, NULL, NULL); if (ret < 0){ av_log(NULL, AV_LOG_ERROR, "不能開啟檔案\n"); return; } ret = avformat_find_stream_info(avf_cxt, NULL); if (ret < 0){ av_log(NULL, AV_LOG_ERROR, "找不到流資料\n"); goto _end; } //列印視訊資訊 av_dump_format(avf_cxt, 0, filename, 0); int video_index = -1; for (int i = 0; i < avf_cxt->nb_streams; i++){ if (avf_cxt->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){ video_index = i; break; } } if (video_index == -1){ av_log(NULL, AV_LOG_ERROR, "沒有找到視訊流\n"); goto _end; } AVCodecContext* avc_cxt = avf_cxt->streams[video_index]->codec; enum AVCodecID codecId = avc_cxt->codec_id; AVCodec* codec = avcodec_find_decoder(codecId); if (!codec){ av_log(NULL, AV_LOG_ERROR, "沒有找到解碼器\n"); goto _end; } ret = avcodec_open2(avc_cxt, codec, NULL); if (ret < 0){ av_log(NULL, AV_LOG_ERROR, "解碼器無法開啟\n"); goto _end; } //為avpacket分配記憶體 AVPacket* packet = av_packet_alloc(); //為avFrame分配記憶體 AVFrame* frame = av_frame_alloc(); while (av_read_frame(avf_cxt, packet) >= 0){ if (packet && packet->stream_index == video_index){ int gotFrame = 0; ret = avcodec_decode_video2(avc_cxt, frame, &gotFrame, packet); if (gotFrame){ ret = writeJPEG(frame, avc_cxt->width, avc_cxt->height); if (ret == 0){ break; } } } } _end: av_frame_free(&frame); avcodec_close(avc_cxt); avformat_free_context(avf_cxt); // return 0; }

來自:

https://www.iteye.com/blog/cmzx3444-965792
https://zhuanlan.zhihu.com/p/37697332
https://blog.csdn.net/qqqq245425070/article/details/87529209