Record rtmp stream to multi flv files - h.264

I'm currently write rtmp server to receive rtmp stream, then record to multi flv file, segment base on time.
Example: 1 minute -> 1 flv file, 2m -> 2 flv file...
Problem: only the first flv file is playable, from the second onwards, they are not playable, maybe they miss some metadata of the codec (h264).
How can I resolve that problem?

Yes, there is a header, as well as sequence headers depending on the codec used. The segments must also be split on keyframes. The FLV formate is well documented here https://www.adobe.com/content/dam/acom/en/devnet/flv/video_file_format_spec_v10.pdf

Related

AAC & AVC byte stream data into ts file

Background:
I am trying to build my own RTMP server for live streaming. (Python3+)
I have succeeded to establish an RTMP connection between the server and the client (using OBS). The client sends me the video and audio data. (audio codec: AAC, video codec: AVC).
I also built a website (HTTP), with HLS (hls.js) player (.TS & .M3U8) that works properly.
Goal:
What I need to do is to take the video and audio data (bytes) from the RTMP connection and join them into a .ts file container so it could be playable on my website.
Plan:
I planned to join them into an FLV file and then convert it to TS using FFmpeg. which actually worked with the audio data because FLV support AAC, so I generated FLV files that contained audio-only.
Problem:
The problem started when I tried to add the video data into the FLV file, but it is not possible because FLV does not support AVC.
What I tried to do:
I can choose another file container to work with, that supports AVC/AAC but they all seem really complicated and I don't have much time for this.
I tried to find a way to convert the AVC to H.263 and then put it inside the FLV (Because it does support H263) but I couldn't find a way.
I really need help. Thanks in advance!

How does the HTML5 audio/video tag determine duration

The HTML5 <audio> and <video> tags support a duration property. I am curious how, without downloading the entire audio/video source file is the browser able to determine the duration of the media?
I am asking because I would like to implement the same functionality in a backend service I am writing that will:
Accept a url for an mp3
Determine the length (in seconds) of the file, without downloading the entire file
Most video containers contain both the visual and audio elements and also a metadata block that describes things like the duration, the colorspace, the codecs used and the offset for each frame (useful when seeking). In a typical video encoded for the web as MP4 this block (aka the MOOV atom) defaults to the end of the file (as the frame location won't be known until the end) unless a second pass has been performed to move it to the front eg:
ffmpeg -i source.mp4 -c:a copy -c:v copy -movflags faststart destination.mp4
(copies the audio and video unchanged, just moves the metadata to the start to enable faster access)
You might have experienced some web video where you can seek almost immediately with an MP4 and some where you can't accurately seek until the file has been fully loaded... this is because the browser has to make 'guesses' until it receives that metadata
For mp3 files specifically you could use something like this - to request the server gives you just the ID3 Tag and eTag data (last 127 and the 227 bytes) without having to download the whole file

Export dynamic audio from Flash

I'm generating a 44100hz dynamic audio stream in Flash using a flash.media.Sound object and the SAMPLE_DATA event. I'd like to be able to analyze the output instead of just listening to it.
What would be the most straightforward way of converting my Flash stream of float samples to an audio file, in a standard format that can be opened by an audio editor? Is there any audio format that would be particularly suitable for this?
If you don't want to listen to it, there's no need to use Sound or the Event.SAMPLE_DATA at all. Just create the numbers and store them in a ByteArray or other data structure.
Is there any audio format that would be particularly suitable for this?
A format that can be opened by your audio editor would be preferable.
Otherwise, this totally depends on what you want to do with the sound data.
What would be the most straightforward way of converting my Flash stream of float samples to an audio file, in a standard format that can be opened by an audio editor?
To use an existing library that encodes the data into the specified format.
tonfall supports "various audio formats Wav AIFF RAW PCM (no header) "Encoder/Decoder
WaveEncoder from Nicolas Bretin apparently encodes to WAV
Of course, if you know the specification, you can write your own encoder.

Reconstructing fragmented H.264 stream

I have H.264 stream stored as a file. I am trying to create a MPEG4 file by adding this stream to MDAT BOX. I have created other headers required by MPEG-4 standards. But I am unable to read the MPEG-4 file.
I parsed the H.264 stream and I see that there are multiple I-frames in the file. It seems to me that this is fragmented H.264 stream.
Is there any way in which this fragmented H.264 stream can be combined into a single I-frame?
I have gone through the link Problem to Decode H264 video over RTP with ffmpeg (libavcodec).
I implemented what was mentioned in the link but i am still unable to run the MPEG-4 thus created.
With the above technique, I get the fragmentType = 5. I get the following nalTypes (8, 2, 1, 0, 0, ...). I get the startBit as specified and for the other fragments, I get the 00 (for StartBit|endBit). I do not get the endBit.
When i try using FFMPEG to reconvert the MPEG-4 file that was created, i get the following error: "header damaged". It looks like the reconstruction of IDR frames is not working properly.
Please let me know if the method that I am following has any issues.
The H.264 stream file is around 100KB. When this file is converted to MP4 using FFMPEG, I get around 38KB. Does it mean that FFMPEG is re-encoding the file once again inorder to recreate the MP4 file?
With the above technique that was mentioned in the link, the MP4 that I have created is around 100KB itself.
Please let me know what I am doing that is wrong.
Thanks in advance.
It sounds like you'd like to wrap an H.264 elementary stream in a mp4 container so that you can play it back.
A tool like mp4box (http://gpac.wp.mines-telecom.fr/mp4box/) will enable you to wrap you elementary stream into a mp4 file. For example:
mp4box -add MySourcFile.h264 MyDestFile.mp4

Converting Mpeg file to flv and simultaneously playing the converted file in flash cs3 using as3

I have an mpg file which I want to convert to flv format, but I have a requirement that while converting the mpg file, I also have to simultaneously play the converted flv file in the flash cs3. How to do it? I am using cs3 and as3.
If you want to convert your files programmaticly then use ffmpeg. This is a commandline tool which can convert video files to nearly everything. You have to execute ffmpeg with the correct params and wait until the video is ready. This works only on serverside. Means the flash client loads up the video file to the server. There it gets converted. You can execute ffmpeg with any serverside language like php.
Sadly I have no idea if it is possible to watch the video while converting. I think not but maybe someone else knows more.