<div>Hi, all.</div><div>š</div><div>I am newbe in this mail-list. I want to record RTSP-stream into files via libavformat / libavcodec programmaticaly, without trans-coding.</div><div>š</div><div>Here is the code (how i trying to record stream, without error checking):</div><div>š</div><div><div><span style="font-family:"courier new",courier;">š š ff_FormatContext = avformat_alloc_context();</span></div><div><span style="font-family:"courier new",courier;">š š ff_Dict = NULL;</span></div><div><span style="font-family:"courier new",courier;">š š av_dict_set(&ff_Dict, "rtsp_transport", "tcp", 0);</span></div><div><span style="font-family:"courier new",courier;">š š avformat_open_input(&ff_FormatContext, vs_stream_url_c, NULL, &ff_Dict);</span></div><div><span style="font-family:"courier new",courier;">š š avformat_find_stream_info(ff_FormatContext, NULL);</span></div><div><span style="font-family:"courier new",courier;">š š vs_index = -1;</span></div><div><span style="font-family:"courier new",courier;">š š for (vs_iterator = 0; vs_iterator < ff_FormatContext->nb_streams; vs_iterator++)</span></div><div><span style="font-family:"courier new",courier;">š š { if (ff_FormatContext->streams[vs_iterator]->codec->coder_type == AVMEDIA_TYPE_VIDEO)</span></div><div><span style="font-family:"courier new",courier;">š š š {</span></div><div><span style="font-family:"courier new",courier;">š š š š vs_index = vs_iterator;</span></div><div><span style="font-family:"courier new",courier;">š š š š break;</span></div><div><span style="font-family:"courier new",courier;">š š š }</span></div><div><span style="font-family:"courier new",courier;">š š }</span></div><div><span style="font-family:"courier new",courier;">š š if (vs_index == -1){</span></div><div><span style="font-family:"courier new",courier;">š š š š printf("[ THREAD ] Error : Video streams not founded.\n");</span></div><div><span style="font-family:"courier new",courier;">š š š š return -1;</span></div><div><span style="font-family:"courier new",courier;">š š }</span></div><div><span style="font-family:"courier new",courier;">š š ff_CodecContext = ff_FormatContext->streams[vs_index]->codec;</span></div><div><span style="font-family:"courier new",courier;">š š ff_Codec = avcodec_find_decoder(ff_CodecContext->codec_id);</span></div><div><span style="font-family:"courier new",courier;">šš</span></div><div><span style="font-family:"courier new",courier;">š š avcodec_open2(ff_CodecContext, ff_Codec, NULL) < 0)</span></div><div><span style="font-family:"courier new",courier;">š šš</span></div><div><span style="font-family:"courier new",courier;">š š ff_Frame = avcodec_alloc_frame();</span></div><div><span style="font-family:"courier new",courier;">š š numBytes = avpicture_get_size(ff_CodecContext->pix_fmt, ff_CodecContext->width, ff_CodecContext->height);</span></div><div><span style="font-family:"courier new",courier;">š š buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));</span></div><div><span style="font-family:"courier new",courier;">š š avpicture_fill((AVPicture *)ff_Frame, buffer, ff_CodecContext->pix_fmt, ff_CodecContext->width, ff_CodecContext->height);</span></div><div><span style="font-family:"courier new",courier;">š š av_init_packet(&ff_Packet);</span></div><div><span style="font-family:"courier new",courier;">š š av_read_play(ff_FormatContext);</span></div><div><span style="font-family:"courier new",courier;">š šš</span></div><div><span style="font-family:"courier new",courier;"><span style="white-space:pre;"> </span>secondmeter1 = time(0);</span></div><div>š</div><div><span style="font-family:"courier new",courier;">š š /* Creating output format */</span></div><div><span style="font-family:"courier new",courier;">š š ff_OutputFormat = av_guess_format("mp4", NULL, NULL);</span></div><div><span style="font-family:"courier new",courier;">š š ff_OutputFormatContext = avformat_alloc_context();</span></div><div><span style="font-family:"courier new",courier;">š š ff_OutputFormatContext->oformat = ff_OutputFormat;</span></div><div><span style="font-family:"courier new",courier;">š š ff_OutputStream = avformat_new_stream(ff_OutputFormatContext, NULL);</span></div><div><span style="font-family:"courier new",courier;">š š avcodec_copy_context(ff_OutputStream->codec, ff_CodecContext);</span></div><div><span style="font-family:"courier new",courier;">š š ff_OutputStream->codec->flags = CODEC_FLAG_GLOBAL_HEADER;</span></div><div><span style="font-family:"courier new",courier;">š š ff_OutputStream->codec->time_base.den = ff_CodecContext->time_base.den;</span></div><div><span style="font-family:"courier new",courier;">š š ff_OutputStream->codec->time_base.num = ff_CodecContext->time_base.num;</span></div><div><span style="font-family:"courier new",courier;">š š outputFileName = new char[0];</span></div><div><span style="font-family:"courier new",courier;">š š sprintf(outputFileName, "%svideo_%015d.mp4", vs_stream_savepath_c, (int)secondmeter1);</span></div><div><span style="font-family:"courier new",courier;">š š frameCounter = 0;</span></div><div><span style="font-family:"courier new",courier;">š š avio_open2(&ff_OutputFormatContext->pb, (const char*)outputFileName, AVIO_FLAG_WRITE,NULL,NULL);</span></div><div><span style="font-family:"courier new",courier;">š š int ix = 0;</span></div><div><span style="font-family:"courier new",courier;">š š while (1 == 1)</span></div><div><span style="font-family:"courier new",courier;">š š {</span></div><div><span style="font-family:"courier new",courier;">š š š š av_read_frame(ff_FormatContext, &ff_Packet);</span></div><div><span style="font-family:"courier new",courier;">š š š š if(ff_Packet.stream_index == vs_index){</span></div><div><span style="font-family:"courier new",courier;">š š š š š š printf(" DATA : PTS = %d DTS = %d DURATION = %d\n", ff_Packet.pts, ff_Packet.dts, ff_Packet.duration);</span></div><div><span style="font-family:"courier new",courier;">š š š š š š // I CAN NOT UNDERSTAND, HOW I'D FILL THIS STRUCTS BEFORE FRAME WRITING</span></div><div><span style="font-family:"courier new",courier;">š š š š š š ff_Packet.stream_index = ff_OutputStream->id;</span></div><div><span style="font-family:"courier new",courier;">š š š š š š ff_OutputStream->pts.val š = ff_Packet.pts;</span></div><div><span style="font-family:"courier new",courier;">š š š š š š ff_OutputStream->duration š= ff_Packet.duration;</span></div><div><span style="font-family:"courier new",courier;">š š š š š š ff_OutputStream->r_frame_rate = ff_FormatContext->streams[vs_index]->r_frame_rate;</span></div><div>š</div><div><span style="font-family:"courier new",courier;">š š š š š š if (frameCounter == 0) avformat_write_header(ff_OutputFormatContext, NULL);</span></div><div><span style="font-family:"courier new",courier;">š š š š š š av_write_frame(ff_OutputFormatContext, &ff_Packet);</span></div><div><span style="font-family:"courier new",courier;">š š š š š š frameCounter++;</span></div><div><span style="font-family:"courier new",courier;">š š š š š š if (frameCounter == 512)</span></div><div><span style="font-family:"courier new",courier;">š š š š š š {</span></div><div><span style="font-family:"courier new",courier;">š š š š š š š š frameCounter = 0;</span></div><div><span style="font-family:"courier new",courier;">š š š š š š š š av_write_trailer(ff_OutputFormatContext);</span></div><div><span style="font-family:"courier new",courier;">š š š š š š š š avio_close(ff_OutputFormatContext->pb);</span></div><div><span style="font-family:"courier new",courier;">š š š š š š š š secondmeter1 = time(0);</span></div><div>š</div><div><span style="font-family:"courier new",courier;">š š š š š š š š sprintf(outputFileName, "%svideo_%015d.mp4", vs_stream_savepath_c, (int)secondmeter1);</span></div><div><span style="font-family:"courier new",courier;">š š š š š š š š avio_open2(&ff_OutputFormatContext->pb, (const char*)outputFileName, AVIO_FLAG_WRITE,NULL,NULL);</span></div><div><span style="font-family:"courier new",courier;">š š š š š š }</span></div><div><span style="font-family:"courier new",courier;">š š š š }</span></div><div><span style="font-family:"courier new",courier;">š š š š av_free_packet(&ff_Packet);</span></div><div><span style="font-family:"courier new",courier;">š š š š av_init_packet(&ff_Packet);</span></div><div><span style="font-family:"courier new",courier;">š š š š #ifdef __MINGW32__</span></div><div><span style="font-family:"courier new",courier;">š š š š š š Sleep(50);</span></div><div><span style="font-family:"courier new",courier;">š š š š #endif // _WIN32</span></div><div><span style="font-family:"courier new",courier;">š š }</span></div></div><div>š</div><div>Code writes video by segments (every 512 frame - new segment with timestamp in filename)</div><div>Into output file i see wrong duration / FPS information. Video playing so fast.</div><div>Please help. :(</div><div>š</div><div>-- <br /><div><strong>๓ ีืมึลฮษลอ, ๅืวลฮษส ็มาษฦีฬฬษฮ.</strong></div><div>e-mail: <a href="mailto:grisme@yandex.ru">grisme@yandex.ru</a></div><div>jabber: <a href="mailto:joffadark@jabber.ufanet.ru">joffadark@jabber.ufanet.ru</a></div><div>ICQ : 387906261</div></div>