Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate FLV stream from raw h264 which can be played by Actionscript NetStream?

Tags:

h.264

flv

I have an issue with FLV stream generating. I've developed DVR system, an it should be able to stream video in FLV format (to play it on Actionscript NetStream). I receive video from encoder in raw H264 NAL units (0x00 0x00 0x00 0x01 ), also I can recognize is encoded frame IDR or non-IDR.

My solution to create FLV stream (based on Adobe spec: Video File Format Specification Version 10) was:

  1. wait for IDR frame;
  2. put FLV header
  3. put PrevTagSize(0)
  4. put FLV tag with video tag VIDEODATA with AVCVIDEODATA
  5. put PrevTagSize
  6. repeat steps 4,5 till end of streaming.

Stream looks good, and can be playable by ffplay, mplayer, vlc, etc. But not played by player based on Actionscript NetStream.

So, I've get raw h264 data and convert it to FLV using ffmpeg:

ffmpeg -f h264 -i d1.h264 -vcodec copy -f flv d1.flv

and try to compate both flv's my and ffmpeg's.

First of all I see that ffmpeg adds AVC sequence header, immediately after FLV header. I've started to do the same, but NetStream still not support my stream, and also another players ceased to play it.

Ok, then I've continue to compare flv's. Now I see that NAL unit headers in ffmpeg's coded FLV a bit changed, but I can't understand what the meaning of the changes. I read many specs, but nothing helpful. Did anybody can clarify me this?

Fo example my NAL units looks so: 00 00 00 01 XX XX ... - for all units

FFmpeg NALs: 00 00 [14 BA] 61 9A ... - non IDR (two bytes variable) 00 00 [7A 02] 65 88 ... - IDR (two bytes variable) 00 00 00 40 06 05 ... - SEI

Is there added some counter or anything else?

Will be happy to see any ideas, links, etc.

like image 455
Pyih Avatar asked Aug 08 '12 10:08

Pyih


2 Answers

Try to use following command to do the work:

ffmpeg -y -i test.flv -vcodec copy -vbsf h264_mp4toannexb test.h264

You will got a vlc playable .h264 file, all NAL is begin with 00 00 00 01.

like image 125
Ken Avatar answered Nov 11 '22 20:11

Ken


There are two common H.264 bitstream packing formats.

  1. Annex B contains start codes: 00 00 01
  2. MP4 is length prefixed XX XX XX XX

You are creating annex B but it seems like you need mp4 packing format (length prefixed) for FLV. You have to remove (00) 00 00 01 and add the length as prefix.

like image 2
Markus Schumann Avatar answered Nov 11 '22 22:11

Markus Schumann