[Ffmpeg-devel] Real-time mpegts encoding and streaming

Theofilos Karachristos teo_kar
Wed Feb 28 15:44:41 CET 2007

Hi to all

I guess that this is the right place to ask a question regarding the ffmpeg library, right?

I am trying to built a system that captures the output of a game, encodes the data into mpeg2 TS files (using the 
ffmpeg library) and transmits the files via simple udp. What I have achieved till now is to create small TS files on every 
12 frames (GOP size), which the Server opens and closes all the time in order to transmit the data. As you understand, the Disk 
I/O that is required for such operation adds a huge delay on the whole process.

What I want to ask is whether I can use a buffer to encode the data into a single mpeg2 TS and be able to transmit
the already encoded data while other are being encoded (real-time)? If I have understood well, the pipe output formats
are only used for raw images. Thus, I think that I have to use what you call a feed or something else. 

If I don't specify a filename to the the AVFormatContext, will it write the encoded TS data on a buffer, and, if yes,
how will my server be able to read this buffer?

Can anybody clarify to me what functions and formats I can use for such operation, because I got really lost while 
trying to follow the steps that ffmpeg uses and understand how it works. Also, I tried to study the ffserver code, but 
understood only a portion of its functionality.

The most inportant parts of my code are the following:

/* Initialization */

sprintf_s( outTSFile, 100,"D:\\Tmp\\capture.ts");
pOutFormat = guess_format(NULL, (const char *) outTSFile, NULL);

pFormatCtx = av_alloc_format_context();

pFormatCtx->oformat = pOutFormat;

_snprintf( pFormatCtx->filename, sizeof( pFormatCtx->filename ), "%s", outTSFile );

if ( pOutFormat->video_codec != CODEC_ID_NONE ) {
    pVideoStream = av_new_stream( pFormatCtx, 0 );
    pCodecCtx = pVideoStream->codec;
    pCodecCtx ->codec_id = pOutFormat->video_codec;
    ...some other code parameters...

av_set_parameters( pFormatCtx, NULL );

pCodec = avcodec_find_encoder( pVideoStream->codec->codec_id );

avcodec_open( pCodecCtx, pCodec );

pAVFrame = avcodec_alloc_frame();

avpicture_fill( ( AVPicture * ) pAVFrame, frameBuf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height );

url_fopen( &pFormatCtx->pb, (const char *) outTSFile, URL_WRONLY )

av_write_header( pFormatCtx );

/* Encoding */

outputSize = avcodec_encode_video( pCodecCtx, videoOutBuf, videoOutBufSize, pAVFrame )

if ( outputSize > 0 )
    AVPacket pPacket;


    pPacket.pts= av_rescale_q( pCodecCtx->coded_frame->pts, pCodecCtx->time_base, pVideoStream->time_base);
    if( pCodecCtx->coded_frame->key_frame )
        pPacket.flags |= PKT_FLAG_KEY;
    pPacket.stream_index = pVideoStream->index;
    pPacket.data = videoOutBuf;
    pPacket.size = outputSize;

    av_write_frame( pFormatCtx, &pPacket);


Thanx in advance and sorry for the long post


 Now you can scan emails quickly with a reading pane. Get the new Yahoo! Mail.

More information about the ffmpeg-devel mailing list