1. 程式人生 > >FFmpeg 獲取RTSP傳過來的視訊資料並儲存成檔案

FFmpeg 獲取RTSP傳過來的視訊資料並儲存成檔案

  廢話不多說,直接上程式碼。

  需要注意的是,FFmpeg的版本很多,最新版本可能有些函式已經換成別的了。如果無法自行更改程式碼,可以找我以前相關FFmpeg的文章,下載我x64版本的工程包,裡面就有這個版本的FFmpeg。

#ifndef INT64_C 
#define INT64_C(c) (c ## LL) 
#define UINT64_C(c) (c ## ULL) 
#endif 

extern "C" {
    /*Include ffmpeg header file*/
#include <libavformat/avformat.h> 
#include <libavcodec/avcodec.h> 
#include <libswscale/swscale.h> #include <libavutil/imgutils.h> #include <libavutil/opt.h> #include <libavutil/mathematics.h> #include <libavutil/samplefmt.h> } void main() { //變數 AVFormatContext *pFormatCtx; char filepath[] = "rtsp://192.168.50.171/live/stream0"
; AVPacket *packet; //初始化 av_register_all(); avformat_network_init(); pFormatCtx = avformat_alloc_context(); AVDictionary* options = NULL; av_dict_set(&options, "buffer_size", "102400", 0); //設定快取大小,1080p可將值調大 av_dict_set(&options, "rtsp_transport", "tcp", 0); //以udp方式開啟,如果以tcp方式開啟將udp替換為tcp
av_dict_set(&options, "stimeout", "2000000", 0); //設定超時斷開連線時間,單位微秒 av_dict_set(&options, "max_delay", "500000", 0); //設定最大時延 packet = (AVPacket *)av_malloc(sizeof(AVPacket)); //開啟網路流或檔案流 if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) { printf("Couldn't open input stream.\n"); return; } //查詢碼流資訊 if (avformat_find_stream_info(pFormatCtx, NULL)<0) { printf("Couldn't find stream information.\n"); return; } //查詢碼流中是否有視訊流 int videoindex = -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; } //儲存一段時間的視訊流,寫入檔案中 FILE *fpSave; fopen_s(&fpSave, "geth264.h264", "wb"); for (int i = 0; i < 1000; i++) { if (av_read_frame(pFormatCtx, packet) >= 0) { if (packet->stream_index == videoindex) { fwrite(packet->data, 1, packet->size, fpSave);//寫資料到檔案中 } av_packet_unref(packet); } } //釋放記憶體 fclose(fpSave); av_free(pFormatCtx); av_free(packet); }