Search code examples
c#ffmpegvideo-streamingmp4rtsp

How to convert an IP Camera video stream into a video file?


I have a URL (<ip>/ipcam/mpeg4.cgi) which points to my IP camera which is connected via Ethernet. Accessing the URL resuls in a infinite stream of video (possibly with audio) data.

I would like to store this data into a video file and play it later with a video player (HTML5's video tag is preferred as the player).

However, a straightforward approach, which is simple saving the stream data into .mp4 file, didn't work.

I have looked into the file and here is what I saw (click to enlarge):

It turned out, there are some HTML headers, which I further on manually excluded using the binary editing tool, and yet no player could play the rest of the file.

The HTML headers are:

--myboundary
Content-Type: image/mpeg4
Content-Length: 76241
X-Status: 0
X-Tag: 1693923
X-Flags: 0
X-Alarm: 0
X-Frametype: I
X-Framerate: 30
X-Resolution: 1920*1080
X-Audio: 1
X-Time: 2000-02-03 02:46:31
alarm: 0000

My question is pretty clear now, and I would like any help or suggestion. I suspect, I have to manually create some MP4 headers myself based on those values above, however, I fail to understand format descriptions such as these.

I have the following video stream settings on my IP camera (click to enlarge):

I could also use the ffmpeg tool, but no matter how I try and mix the arguments to the program, it keeps telling me this error:


Solution

  • It looks like your server is sending H.264 encoded 'rawvideo' in Annex B byte stream format.

    It might be reformatted to .mp4 with something like below command line:

    ffmpeg -i {input file} -f rawvideo -bsf h264_mp4toannexb -vcodec copy out.mp4
    

    Saving audio/video streaming into file is not an easy job. If it's video only, using MPEG2 TS format is easiest way to go.

    For .mp4 streaming, consider -movflags faststart -> Recommendation on the best quality/performance H264 encoder for video encoding?

    ** Update: -bsf h264_mp4toannexb option could be omitted, I'm not sure.