2013-02-28 44 views
0

祝您有個美好的一天,人們!如何使用ffmpeg和C++將自制流發佈到rtmp服務器?

我正在爲Windows編寫一個應用程序,它將捕獲屏幕並通過rtmp(用於廣播)將流發送到Wowza服務器。我的應用程序使用ffmpeg和Qt。 我捕獲與WINAPI屏幕,緩衝器轉換成YUV444(因爲它是最簡單的)和編碼幀作爲在文件decoding_encoding.c描述(來自FFmpeg的示例):

/////////////////////////// 
//Encoder initialization 
/////////////////////////// 
avcodec_register_all(); 
codec=avcodec_find_encoder(AV_CODEC_ID_H264); 
c = avcodec_alloc_context3(codec); 
c->width=scr_width; 
c->height=scr_height; 
c->bit_rate = 400000; 
int base_num=1; 
int base_den=1;//for one frame per second 
c->time_base= (AVRational){base_num,base_den}; 
c->gop_size = 10; 
c->max_b_frames=1; 
c->pix_fmt = AV_PIX_FMT_YUV444P; 
av_opt_set(c->priv_data, "preset", "slow", 0); 

frame = avcodec_alloc_frame(); 
frame->format = c->pix_fmt; 
frame->width = c->width; 
frame->height = c->height; 

for(int counter=0;counter<10;counter++) 
{ 
/////////////////////////// 
//Capturing Screen 
/////////////////////////// 
    GetCapScr(shotbuf,scr_width,scr_height);//result: shotbuf is filled by screendata from HBITMAP 
/////////////////////////// 
//Convert buffer to YUV444 (standard formula) 
//It's handmade function because of problems with prepare buffer to swscale from HBITMAP 
/////////////////////////// 
    RGBtoYUV(shotbuf,frame->linesize,frame->data,scr_width,scr_height);//result in frame->data 
/////////////////////////// 
//Encode Screenshot 
/////////////////////////// 
    av_init_packet(&pkt); 
    pkt.data = NULL; // packet data will be allocated by the encoder 
    pkt.size = 0; 
    frame->pts = counter; 
    avcodec_encode_video2(c, &pkt, frame, &got_output); 
    if (got_output) 
    { 
     //I think that sending packet by rtmp must be here! 
     av_free_packet(&pkt);    

    } 

} 
// Get the delayed frames 
for (int got_output = 1,i=0; got_output; i++) 
{ 
    ret = avcodec_encode_video2(c, &pkt, NULL, &got_output); 
    if (ret < 0) 
     { 
      fprintf(stderr, "Error encoding frame\n"); 
      exit(1); 
     } 
     if (got_output) 
     { 
     //I think that sending packet by rtmp must be here! 
     av_free_packet(&pkt);  
     } 
} 

/////////////////////////// 
//Deinitialize encoder 
/////////////////////////// 
avcodec_close(c); 
av_free(c); 
av_freep(&frame->data[0]); 
avcodec_free_frame(&frame); 

我需要發送由生成的視頻流這段代碼到RTMP服務器。 換句話說,我需要C++/C模擬這個命令:

ffmpeg -re -i "sample.h264" -f flv rtmp://sample.url.com/screen/test_stream 

這是非常有用的,但我不希望保存流文件,我想使用實時編碼,屏幕捕獲和發送的ffmpeg庫編碼幀到我自己的應用程序中的RTMP服務器。 請給我一個小例子,如何正確初始化AVFormatContext並將我的編碼視頻AVPackets發送到服務器。

謝謝。

回答

5

我的問題可以通過使用ffmpeg的源代碼來解決。文件muxing.c是必要的。它位於ffmpeg源文件夾ffmpeg\docs\examples中。將樣本流寫入rtmp服務器或文件需要所有源代碼。我只能理解這些來源並添加我自己的流數據而不是樣本流。 可能會有意想不到的問題,但總的來說 - 有一個解決方案。

+0

你最終找到了一個工作解決方案嗎? – Sebastian 2016-11-21 15:44:01

相關問題