ffmpeg 把视频解码成jpg
生活随笔
收集整理的這篇文章主要介紹了
ffmpeg 把视频解码成jpg
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這是一個h264視頻解碼的小demo,先把h264轉成yuv,然后再把yuv轉成jpg
#include <iostream>int Frame2JPG(AVPacket packet, AVFrame* pFrame, unsigned int stream_index,int width, int height) {// 輸出文件路徑char out_file[100] = { 0 };sprintf(out_file,"%s%d.jpg", "video/", packet.pts);// 分配AVFormatContext對象AVFormatContext* pFormatCtx = avformat_alloc_context();// 設置輸出文件格式pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);// 創建并初始化一個和該url相關的AVIOContextif (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0){return -1;}// 構建一個新streamAVStream* pAVStream = avformat_new_stream(pFormatCtx, 0);if (pAVStream == NULL){return -1;}// 設置該stream的信息AVCodecContext* pCodecCtx = pAVStream->codec;pCodecCtx->codec_id = pFormatCtx->oformat->video_codec;pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;pCodecCtx->width = width;pCodecCtx->height = height;pCodecCtx->time_base.num = 1;pCodecCtx->time_base.den = 25;// Begin Output some information// av_dump_format(pFormatCtx, 0, out_file, 1);// End Output some information// 查找解碼器AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);if (!pCodec){return -1;}// 設置pCodecCtx的解碼器為pCodecif (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){return -1;}//Write Headerint ret = avformat_write_header(pFormatCtx, NULL);if (ret < 0){return -1;}int y_size = pCodecCtx->width * pCodecCtx->height;//Encode// 給AVPacket分配足夠大的空間AVPacket pkt;av_new_packet(&pkt, y_size * 3);int got_picture = 0;ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_picture);if (ret < 0){return -1;}if (got_picture == 1){//pkt.stream_index = pAVStream->index;ret = av_write_frame(pFormatCtx, &pkt);}av_free_packet(&pkt);//Write Trailerav_write_trailer(pFormatCtx);if (pAVStream){avcodec_close(pAVStream->codec);}avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);return 0; }int decode_h264(char *filepath) {AVFormatContext *pFormatCtx;AVCodecContext *pCodecCtx;AVCodec *pCodec;AVFrame *pFrame, *pFrameYUV;AVPacket *packet;struct SwsContext *img_convert_ctx;uint8_t *out_buffer;int videoindex = -1;int y_size;int ret, got_picture;FILE *fp_yuv = fopen("1.yuv", "wb+");av_register_all();//avformat_network_init();pFormatCtx = avformat_alloc_context();if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0){printf("Couldn't open input stream.\n");return -1;}if (avformat_find_stream_info(pFormatCtx, NULL) <0){printf("Couldn't find stream information.\n");return -1;}for (int i = 0; i < pFormatCtx->nb_streams; i++){if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){videoindex = i;break;}}if (videoindex == -1){printf("Didn't find a video stream.\n");return -1;}pCodecCtx = pFormatCtx->streams[videoindex]->codec;// 根據編碼器的ID查找FFmpeg的解碼器pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL){printf("Codec not found.\n");return -1;}// 初始化一個視音頻編解碼器的AVCodecContextif (avcodec_open2(pCodecCtx, pCodec, NULL)<0){printf("Could not open codec.\n");return -1;}// 一些內存分配packet = (AVPacket *)av_malloc(sizeof(AVPacket));pFrame = av_frame_alloc();pFrameYUV = av_frame_alloc();out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));// 為已經分配空間的結構體AVPicture掛上一段用于保存數據的空間// AVFrame/AVPicture有一個data[4]的數據字段,buffer里面存放的只是yuv這樣排列的數據,// 而經過fill 之后,會把buffer中的yuv分別放到data[0],data[1],data[2]中。avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);//Output Info-----------------------------printf("--------------- File Information ----------------\n");av_dump_format(pFormatCtx, 0, filepath, 0);printf("-------------------------------------------------\n");// 初始化一個SwsContext// 參數:源圖像的寬,源圖像的高,源圖像的像素格式,目標圖像的寬,目標圖像的高,目標圖像的像素格式,設定圖像拉伸使用的算法img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);while (av_read_frame(pFormatCtx, packet) >= 0){if (packet->stream_index == videoindex){// 解碼一幀視頻數據。輸入一個壓縮編碼的結構體AVPacket,輸出一個解碼后的結構體AVFrameret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);if (ret < 0){printf("Decode Error.\n");return -1;}if (got_picture){// 轉換像素sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameYUV->data, pFrameYUV->linesize);y_size = pCodecCtx->width * pCodecCtx->height;// 向文件寫入一個數據塊fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Yfwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //Ufwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //VFrame2JPG(*packet, pFrameYUV,packet->stream_index,pCodecCtx->width, pCodecCtx->height);//printf("Succeed to decode 1 frame!\n");}}av_free_packet(packet);}//flush decoder//FIX: Flush Frames remained in Codecwhile (1){ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);if (ret < 0)break;if (!got_picture)break;sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameYUV->data, pFrameYUV->linesize);int y_size = pCodecCtx->width*pCodecCtx->height;fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Yfwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //Ufwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //V//printf("Flush Decoder: Succeed to decode 1 frame!\n");}sws_freeContext(img_convert_ctx);fclose(fp_yuv);av_frame_free(&pFrameYUV);av_frame_free(&pFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0; }int main() {char filepath[] = "1.mp4";decode_h264(filepath);return 0; }https://blog.csdn.net/wushuangge/article/details/83347939
總結
以上是生活随笔為你收集整理的ffmpeg 把视频解码成jpg的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html——黑体、斜体、下划线及删除线
- 下一篇: RTSP播放器网页web无插件直播流媒体