Linux下FFMpeg的简单使用

来源:本站
导读:目前正在解读《Linux下FFMpeg的简单使用》的相关信息,《Linux下FFMpeg的简单使用》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《Linux下FFMpeg的简单使用》的详细说明。
简介:在Linux平台下,使用FFMpeg和SDL2实现一个简单的视频播放器

本地文件播放

// gcc myFFMpeg.c -o myFFMpeg.o -lSDL2main -lSDL2 -lavformat  -lavcodec -lavutil -lm -lz -lswscale#include <stdio.h>#define __STDC_CONSTANT_MACROS#ifdef _WIN32//Windowsextern "C"{#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"#include "libswscale/swscale.h"#include "libavutil/imgutils.h"#include "SDL2/SDL.h"};#else//Linux...#ifdef __cplusplusextern "C"{#endif#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#include <libswscale/swscale.h>#include <SDL2/SDL.h>#include <libavutil/imgutils.h>#ifdef __cplusplus};#endif#endif//Output YUV420P data as a file#define OUTPUT_YUV420P 0int main(int argc, char* argv[]){AVFormatContext*pFormatCtx;inti, videoindex;AVCodecContext*pCodecCtx;AVCodec*pCodec;AVFrame*pFrame,*pFrameYUV;unsigned char *out_buffer;AVPacket *packet;int y_size;int ret, got_picture;struct SwsContext *img_convert_ctx;char filepath[]="sintel.ts";//SDL---------------------------int screen_w=0,screen_h=0;SDL_Window *screen;SDL_Renderer* sdlRenderer;SDL_Texture* sdlTexture;SDL_Rect sdlRect;FILE *fp_yuv;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;}videoindex=-1;//printf("======== pFormatCtx->nb_streams = %d ===========n",pFormatCtx->nb_streams);for(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;}//printf("================== i = %d =========================n",i);pCodecCtx=pFormatCtx->streams[videoindex]->codec;pCodec=avcodec_find_decoder(pCodecCtx->codec_id);if(pCodec==NULL){printf("Codec not found.n");return -1;}if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){printf("Could not open codec.n");return -1;}pFrame=av_frame_alloc();pFrameYUV=av_frame_alloc();out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,  pCodecCtx->width, pCodecCtx->height,1));av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);packet=(AVPacket *)av_malloc(sizeof(AVPacket));//Output Info-----------------------------printf("--------------- File Information ----------------n");av_dump_format(pFormatCtx,0,filepath,0);printf("-------------------------------------------------n");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);#if OUTPUT_YUV420P    fp_yuv=fopen("output.yuv","wb+");#endifif(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {printf( "Could not initialize SDL - %sn", SDL_GetError());return -1;}screen_w = pCodecCtx->width;screen_h = pCodecCtx->height;//SDL 2.0 Support for multiple windowsscreen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,screen_w, screen_h,SDL_WINDOW_OPENGL);if(!screen) {printf("SDL: could not create window - exiting:%sn",SDL_GetError());return -1;}sdlRenderer = SDL_CreateRenderer(screen, -1, 0);//IYUV: Y + U + V  (3 planes)//YV12: Y + V + U  (3 planes)sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);sdlRect.x=0;sdlRect.y=0;sdlRect.w=screen_w;sdlRect.h=screen_h;//SDL End----------------------while(av_read_frame(pFormatCtx, packet)>=0){if(packet->stream_index==videoindex){ret = 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 unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameYUV->data, pFrameYUV->linesize);//SDL---------------------------SDL_UpdateYUVTexture(sdlTexture, &sdlRect,pFrameYUV->data[0], pFrameYUV->linesize[0],pFrameYUV->data[1], pFrameYUV->linesize[1],pFrameYUV->data[2], pFrameYUV->linesize[2]);SDL_RenderClear( sdlRenderer );SDL_RenderCopy( sdlRenderer, sdlTexture,  NULL, &sdlRect);SDL_RenderPresent( sdlRenderer );//SDL End-----------------------//Delay 40msSDL_Delay(40);}}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 unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameYUV->data, pFrameYUV->linesize);//SDL---------------------------SDL_UpdateTexture( sdlTexture, &sdlRect, pFrameYUV->data[0], pFrameYUV->linesize[0] );SDL_RenderClear( sdlRenderer );SDL_RenderCopy( sdlRenderer, sdlTexture,  NULL, &sdlRect);SDL_RenderPresent( sdlRenderer );//SDL End-----------------------//Delay 40msSDL_Delay(40);}sws_freeContext(img_convert_ctx);SDL_Quit();av_frame_free(&pFrameYUV);av_frame_free(&pFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0;}

RTP视频播放

// gcc myFFMpeg.c -o myFFMpeg.o -lSDL2main -lSDL2 -lavformat  -lavcodec -lavutil -lm -lz -lswscale#include <stdio.h>#define __STDC_CONSTANT_MACROS#ifdef _WIN32//Windowsextern "C"{#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"#include "libswscale/swscale.h"#include "libavutil/imgutils.h"#include "SDL2/SDL.h"};#else//Linux...#ifdef __cplusplusextern "C"{#endif#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#include <libswscale/swscale.h>#include <SDL2/SDL.h>#include <libavutil/imgutils.h>#ifdef __cplusplus};#endif#endif//Output YUV420P data as a file#define OUTPUT_YUV420P 0int main(int argc, char* argv[]){AVFormatContext*pFormatCtx;inti, videoindex;AVCodecContext*pCodecCtx;AVCodec*pCodec;AVFrame*pFrame,*pFrameYUV;unsigned char *out_buffer;AVPacket *packet;int y_size;int ret, got_picture;struct SwsContext *img_convert_ctx;char filepath[]="sintel.ts";char rtspUrl[]="rtsp://218.204.223.237:554/live/1/67A7572844E51A64/f68g2mj7wjua3la7.sdp";//SDL---------------------------int screen_w=0,screen_h=0;SDL_Window *screen;SDL_Renderer* sdlRenderer;SDL_Texture* sdlTexture;SDL_Rect sdlRect;SDL_Event event;FILE *fp_yuv;av_register_all();avformat_network_init();pFormatCtx = avformat_alloc_context();if(avformat_open_input(&pFormatCtx,rtspUrl,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;}videoindex=-1;//printf("======== pFormatCtx->nb_streams = %d ===========n",pFormatCtx->nb_streams);for(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;}//printf("================== i = %d =========================n",i);pCodecCtx=pFormatCtx->streams[videoindex]->codec;pCodec=avcodec_find_decoder(pCodecCtx->codec_id);if(pCodec==NULL){printf("Codec not found.n");return -1;}if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){printf("Could not open codec.n");return -1;}pFrame=av_frame_alloc();pFrameYUV=av_frame_alloc();out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,  pCodecCtx->width, pCodecCtx->height,1));av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);packet=(AVPacket *)av_malloc(sizeof(AVPacket));//Output Info-----------------------------printf("--------------- File Information ----------------n");av_dump_format(pFormatCtx,0,filepath,0);printf("-------------------------------------------------n");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);if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {printf( "Could not initialize SDL - %sn", SDL_GetError());return -1;}screen_w = pCodecCtx->width;screen_h = pCodecCtx->height;//SDL 2.0 Support for multiple windowsscreen = SDL_CreateWindow("RTSP Player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,screen_w, screen_h,SDL_WINDOW_OPENGL);if(!screen) {printf("SDL: could not create window - exiting:%sn",SDL_GetError());return -1;}sdlRenderer = SDL_CreateRenderer(screen, -1, 0);//IYUV: Y + U + V  (3 planes)//YV12: Y + V + U  (3 planes)sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);sdlRect.x=0;sdlRect.y=0;sdlRect.w=screen_w;sdlRect.h=screen_h;//SDL End----------------------while(av_read_frame(pFormatCtx, packet)>=0){if(packet->stream_index==videoindex){ret = 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 unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameYUV->data, pFrameYUV->linesize);//SDL---------------------------SDL_UpdateYUVTexture(sdlTexture, &sdlRect,pFrameYUV->data[0], pFrameYUV->linesize[0],pFrameYUV->data[1], pFrameYUV->linesize[1],pFrameYUV->data[2], pFrameYUV->linesize[2]);SDL_RenderClear( sdlRenderer );SDL_RenderCopy( sdlRenderer, sdlTexture,  NULL, &sdlRect);SDL_RenderPresent( sdlRenderer );//SDL End-----------------------//Delay 40ms//SDL_Delay(20);}}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 unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameYUV->data, pFrameYUV->linesize);//SDL---------------------------SDL_UpdateTexture( sdlTexture, &sdlRect, pFrameYUV->data[0], pFrameYUV->linesize[0] );SDL_RenderClear( sdlRenderer );SDL_RenderCopy( sdlRenderer, sdlTexture,  NULL, &sdlRect);SDL_RenderPresent( sdlRenderer );//SDL End-----------------------//Delay 40ms//SDL_Delay(20);}sws_freeContext(img_convert_ctx);SDL_PollEvent(&event);switch(event.type){case SDL_QUIT:SDL_Quit();exit(0);break;default:break;}//SDL_Quit();av_frame_free(&pFrameYUV);av_frame_free(&pFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0;}

提醒:《Linux下FFMpeg的简单使用》最后刷新时间 2024-03-14 01:00:50,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《Linux下FFMpeg的简单使用》该内容的真实性请自行鉴别。