Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe if you set the following, you will see video playback.</p> <pre><code>packet.flags |= AV_PKT_FLAG_KEY; packet.pts = packet.dts = 0; </code></pre> <p>You should really set packet.flags according to the h264 packet headers. You might try <a href="https://stackoverflow.com/q/1957427">this fellow stack overflowian's</a> suggestion for extracting directly from the stream.</p> <p>If you are also adding audio, then pts/dts is going to be more important. I suggest you study <a href="http://dranger.com/ffmpeg/tutorial05.html" rel="nofollow noreferrer">this tutorial</a></p> <p><strong>EDIT</strong></p> <p>I found time to extract out what is working for me from my test app. For some reason, dts/pts values of zero works for me, but values other than 0 or AV_NOPTS_VALUE do not. I wonder if we have different versions of ffmpeg. I have the latest from <strong>git://git.videolan.org/ffmpeg.git</strong>.</p> <p><strong>fftest.cpp</strong></p> <pre><code>#include &lt;string&gt; #ifndef INT64_C #define INT64_C(c) (c ## LL) #define UINT64_C(c) (c ## ULL) #endif //#define _M #define _M printf( "%s(%d) : MARKER\n", __FILE__, __LINE__ ) extern "C" { #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" }; AVFormatContext *fc = 0; int vi = -1, waitkey = 1; // &lt; 0 = error // 0 = I-Frame // 1 = P-Frame // 2 = B-Frame // 3 = S-Frame int getVopType( const void *p, int len ) { if ( !p || 6 &gt;= len ) return -1; unsigned char *b = (unsigned char*)p; // Verify NAL marker if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] ) { b++; if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] ) return -1; } // end if b += 3; // Verify VOP id if ( 0xb6 == *b ) { b++; return ( *b &amp; 0xc0 ) &gt;&gt; 6; } // end if switch( *b ) { case 0x65 : return 0; case 0x61 : return 1; case 0x01 : return 2; } // end switch return -1; } void write_frame( const void* p, int len ) { if ( 0 &gt; vi ) return; AVStream *pst = fc-&gt;streams[ vi ]; // Init packet AVPacket pkt; av_init_packet( &amp;pkt ); pkt.flags |= ( 0 &gt;= getVopType( p, len ) ) ? AV_PKT_FLAG_KEY : 0; pkt.stream_index = pst-&gt;index; pkt.data = (uint8_t*)p; pkt.size = len; // Wait for key frame if ( waitkey ) if ( 0 == ( pkt.flags &amp; AV_PKT_FLAG_KEY ) ) return; else waitkey = 0; pkt.dts = AV_NOPTS_VALUE; pkt.pts = AV_NOPTS_VALUE; // av_write_frame( fc, &amp;pkt ); av_interleaved_write_frame( fc, &amp;pkt ); } void destroy() { waitkey = 1; vi = -1; if ( !fc ) return; _M; av_write_trailer( fc ); if ( fc-&gt;oformat &amp;&amp; !( fc-&gt;oformat-&gt;flags &amp; AVFMT_NOFILE ) &amp;&amp; fc-&gt;pb ) avio_close( fc-&gt;pb ); // Free the stream _M; av_free( fc ); fc = 0; _M; } int get_nal_type( void *p, int len ) { if ( !p || 5 &gt;= len ) return -1; unsigned char *b = (unsigned char*)p; // Verify NAL marker if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] ) { b++; if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] ) return -1; } // end if b += 3; return *b; } int create( void *p, int len ) { if ( 0x67 != get_nal_type( p, len ) ) return -1; destroy(); const char *file = "test.avi"; CodecID codec_id = CODEC_ID_H264; // CodecID codec_id = CODEC_ID_MPEG4; int br = 1000000; int w = 480; int h = 354; int fps = 15; // Create container _M; AVOutputFormat *of = av_guess_format( 0, file, 0 ); fc = avformat_alloc_context(); fc-&gt;oformat = of; strcpy( fc-&gt;filename, file ); // Add video stream _M; AVStream *pst = av_new_stream( fc, 0 ); vi = pst-&gt;index; AVCodecContext *pcc = pst-&gt;codec; _M; avcodec_get_context_defaults2( pcc, AVMEDIA_TYPE_VIDEO ); pcc-&gt;codec_type = AVMEDIA_TYPE_VIDEO; pcc-&gt;codec_id = codec_id; pcc-&gt;bit_rate = br; pcc-&gt;width = w; pcc-&gt;height = h; pcc-&gt;time_base.num = 1; pcc-&gt;time_base.den = fps; // Init container _M; av_set_parameters( fc, 0 ); if ( !( fc-&gt;oformat-&gt;flags &amp; AVFMT_NOFILE ) ) avio_open( &amp;fc-&gt;pb, fc-&gt;filename, URL_WRONLY ); _M; av_write_header( fc ); _M; return 1; } int main( int argc, char** argv ) { int f = 0, sz = 0; char fname[ 256 ] = { 0 }; char buf[ 128 * 1024 ]; av_log_set_level( AV_LOG_ERROR ); av_register_all(); do { // Raw frames in v0.raw, v1.raw, v2.raw, ... // sprintf( fname, "rawvideo/v%lu.raw", f++ ); sprintf( fname, "frames/frame%lu.bin", f++ ); printf( "%s\n", fname ); FILE *fd = fopen( fname, "rb" ); if ( !fd ) sz = 0; else { sz = fread( buf, 1, sizeof( buf ) - FF_INPUT_BUFFER_PADDING_SIZE, fd ); if ( 0 &lt; sz ) { memset( &amp;buf[ sz ], 0, FF_INPUT_BUFFER_PADDING_SIZE ); if ( !fc ) create( buf, sz ); if ( fc ) write_frame( buf, sz ); } // end if fclose( fd ); } // end else } while ( 0 &lt; sz ); destroy(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload