<p>The way I've seen this done in other systems intended for CCTV is to encode every couple of frames compressed with something like <a href="http://en.wikipedia.org/wiki/Motion_JPEG" rel="nofollow">MJPEG</a> and then stream that to the client, the server component of ffmpeg, <a href="http://www.ffmpeg.org/ffserver.html" rel="nofollow">ffserver</a> may be able to help you with that. 
Of course, depending on your needs a different video codec may be more appropriate, particularly if you intend to send every frame in which case something like H.264 may be more efficient.</p>

CC BY-SA 3.0
2013-11-19T12:50:48.520Z
Hi, that has given me something to think about -thanks
Andrew Simpson
CC BY-SA 3.0
2015-02-26T03:28:50.530Z
A fixed camera is always going to have a LOT of temporal redundancy between frames, so you'll save a lot of bits with any codec that can use P and B frames (inter prediction).
Peter Cordes
CC BY-SA 3.0
2013-11-19T12:21:39.780Z
2013-11-19T12:21:39.780Z
Sam M-G
<p>So you have a client computer with a camera, and want to stream video from it to a server where it's archived and accessible?</p>

<p>client runs:
<code>ffmpeg -i some_kind_of_input  -vf mpdecimate -c:v libx264 -preset medium -crf 25  some_kind_of_output</code></p>

<p>Choose an appropriate framerate to capture.  mpdecimate will run constantly at that framerate.  libx264 with your chosen preset will have to keep up with realtime to avoid dropped frames when there is motion going on, so mpdecimate isn't dropping any duplicate frames.</p>

<p>The output from that should be buffered somehow so upload to the server can lag behind the bitrate of the output when there is a burst of data (because of motion).</p>

<p>Your output will be VFR video.  There will be continuous 10fps or w/e bits, and other bits with 1 frame per hour or something.  x264 doesn't care, and most container formats have no trouble storing timestamps for each frame.  (e.g. mkv or mp4 are probably both ok choices.)</p>

<p>The server receives this video stream, and breaks it into 1 minute or 1 hour chunks or something.  (or maybe it's easier to break into chunks on the client side, and upload a chunk after it's done being written.  That would stop you from getting a realtime feed from your cam though.)</p>

<p>Or server just decodes it and stores jpg images with timestamps for filenames.  I'm not sure what kind of support ffmpeg has for VFR output to the <code>image2</code> muxer (that's the "directory of image files" with <code>%04d.jpg</code> filenames, or w/e.  AFAIK, the <code>%04d</code> can only substitute frame numbers, not timestamps.)</p>

<p>Anyway, I just made all this up.  I'm sure you'll find lots of stuff if you google on motion detection security video.</p>

<p>IDK why this was on the recent-activity questions list, but since I read it, I figured I'd post an answer.</p>

CC BY-SA 3.0
2015-02-26T07:58:34.880Z
I too do not know why this has appeared on the recent-activity list but I am glad it did!  If I can work out how this happened I will attempt to regurgitate all my past questions :)  Also, your answer was A1!
Andrew Simpson
CC BY-SA 3.0
2015-02-26T09:48:43.787Z
Hi,  When i run this command via my C# app I just a get byte values of 255?  What have I missed?
Andrew Simpson
CC BY-SA 3.0
2015-02-26T21:50:02.227Z
you need ffmpeg installed, and `some_kind_of_input` is just a placeholder.  Same for output.  If you want to use it from within  a `C#` app, you might want to use the `libav*` API, instead of piping data through ffmpeg running as an external process.  I haven't used the api myself, but `ffmpeg` (the cmdline program) is just a front-end for `libavformat`, `libavfilter`, `libavcodec`, etc.  Have a look at http://www.ffmpeg.org/documentation.html, and google on using ffmpeg libraries.  (also note that there's a fork of ffmpeg called libav, so you'll get google hits on that.)
Peter Cordes
CC BY-SA 3.0
2015-02-27T00:35:00.793Z
sorry, didn't take as long thinking about your reply as I should have.  So you mean the output file is filled with `0xff` bytes?  Or the `system()` function call returns 255?  I really don't have a very good idea of exactly what you might have done.  You might want to ask a new question (with code and output) on stackoverflow.com (which would be the right place, since you're writing code.)  You might get answers telling you to use the library API instead of trying to pipe your data through the cmdline frontend, though.  If ffmpeg can read directly from the camera, that might be useful...
Peter Cordes
CC BY-SA 3.0
2015-02-27T00:37:57.100Z
Yes, deleted as I thought it was a reply to another of my questions lol. Using mobile to reply, sorry
Andrew Simpson
CC BY-SA 3.0
2015-02-27T00:42:18.600Z
I prob need to post a new question. If use those command arguments in a c# pipe app I get a stream of 255 bytes. Before I got complete images before app stopped. I will need to get to a PC to use this in a dos windows and capture the standards error
Andrew Simpson
CC BY-SA 3.0
2015-02-27T16:48:55.637Z
Peter Thanks for those links you gave me.  the C# is just medium I can use.  It was notiecable that every raw byte was 255 with those command arguments you kindly supplied.  I need to now try and replicate it on the command line and try to get a log file to be able to submit a new question.  It is still ffmpeg specific as opposed to C# specific and I know the C# code is all ok. so just down to those command arguments really..:)
Andrew Simpson
CC BY-SA 3.0
2015-02-26T03:27:10.807Z
2015-02-26T03:27:10.807Z
Peter Cordes
<p>If you are on Windows (presumably since you are using C#) is there a reason that you can't simply use Windows Media Encoder and Windows Streaming Media Services on the server?  It sounds like it is designed to do exactly what you are trying to do.  I'm not sure I understand why you need to build your own solution when many great (and even free or no additional cost) solutions exist already.</p>

<p>Update:
Based on the understanding that you just need about 10fps jpeg's and that latency doesn't matter as much as guaranteeing delivery, I would probably do the following:</p>

<p>1) Client submits each image with an incrementing sequence number to the server.</p>

<p>2) The server takes each image and stores it in to the file system with the path and sequence number in a dictionary.</p>

<p>3) When the viewer connects it starts by asking for the current image and is given the image and sequence number.</p>

<p>4) Upon display of an image, the viewer then requests the next sequence number.  Timing information for how far behind the viewer is could also be sent back to keep it from getting too far behind.  The server would wait until an image is available for the sequence number if it doesn't yet have one.  This would help keep in sync as well.</p>

CC BY-SA 3.0
2015-02-26T03:30:41.557Z
ffmpeg has an input demuxer that can read a stream of image files piped in back-to-back.  `-f image2pipe`.  Or just decode the jpegs, and feed the image data to WME.
Peter Cordes
CC BY-SA 3.0
2013-11-19T14:31:36.433Z
Hi, thanks for replying. I did take a look at Windows media Encoder but it is working with streams.  I am using jpegs you see.
Andrew Simpson
CC BY-SA 3.0
2013-11-19T14:33:09.323Z
@AndrewSimpson - ah, so video (even at low framerate) isn't an option? Would you like to jump in [chat](http://chat.stackexchange.com/rooms/733/audio-video-chat) to discuss a bit more about what you need.  I'm still not 100% sure I understand the scenario.
AJ Henderson
CC BY-SA 3.0
2013-11-19T14:34:11.340Z
hi, I would love to. How do do that?
Andrew Simpson
CC BY-SA 3.0
2013-11-19T14:34:40.157Z
@AndrewSimpson - updated my comment with a link on the word Chat, just click it. :)
AJ Henderson
CC BY-SA 3.0
2013-11-19T14:29:04.290Z
2013-11-19T14:48:45.683Z
2013-11-19T14:48:45.683Z
AJ Henderson
<p>I have developed a desktop application that records motion from any video source - ie ip Camera, web cam (etc).</p>

<p>These motion 'clips' timestamps are uploaded to my server.</p>

<p>The Web user can log in and view these 'Time-clips'.</p>

<p>If they want to view any of these clips they double-click on it a request is made to a clients PC (in this scenario a very old laptop) and the images which are in jpeg format are encoded to ogg video format using ffmpeg.</p>

<p>Everything works well and fast.</p>

<p>Now, the client has complained/wants constant streaming of all the video captured from the client.</p>

<p>My second immediate thoughts was to have a base image on my server (or 2 - for caching)) and update the differences between 2 frames and upload and overlay the pixel changes on the base image on my server.</p>

<p>Now, as I have been informed before this actual process is how a video encoder works?</p>

<p>If that is the case I could save a lot of time using an encoder to change the base image on my server.</p>

<p>ffmpeg naturally comes to mind.</p>

<p>1). Does this sound reasonable to try to achieve?
2). Are they any pointers to what arguments I could use with ffmpeg to achieve all this?</p>

<p>I am very new to this fascinating subject and I am keen to learn and understand rather than copy &amp; paste (sounds like a job interview!).</p>

<p>I am using C# as a programming tool but consider this is irrelevant to the initial question?</p>

<p>I very much welcome people's thoughts.</p>

<p>Thanks!</p>

CC BY-SA 3.0
2015-02-26T03:12:17.357Z
Old question, but as soon as you started talking about detecting pixel differences yourself, I just had to say:  It's not worth it to invent your own video codec.  Use h.264 or something.  Huge amounts of time have been put into optimizing the hell out of x264, for example.  And use ffmpeg's `mpdecimate` to drop similar frames before sending them to a video codec.
Peter Cordes
CC BY-SA 3.0
2015-02-26T07:42:35.003Z
@PeterCordes I appreciate your comments. I have to admit when I originally posted this question I was VERY naive.  Now I am just naive :) Thanks
Andrew Simpson
<ffmpeg>
Constant streaming to a web server
Andrew Simpson
# Constant streaming to a web server

(Asked by: username_0 on 2013-11-19)

I have developed a desktop application that records motion from any video source - ie ip Camera, web cam (etc).

These motion 'clips' timestamps are uploaded to my server.

The Web user can log in and view these 'Time-clips'.

If they want to view any of these clips they double-click on it a request is made to a clients PC (in this scenario a very old laptop) and the images which are in jpeg format are encoded to ogg video format using ffmpeg.

Everything works well and fast.

Now, the client has complained/wants constant streaming of all the video captured from the client.

My second immediate thoughts was to have a base image on my server (or 2 - for caching)) and update the differences between 2 frames and upload and overlay the pixel changes on the base image on my server.

Now, as I have been informed before this actual process is how a video encoder works?

If that is the case I could save a lot of time using an encoder to change the base image on my server.

ffmpeg naturally comes to mind.

1). Does this sound reasonable to try to achieve?
2). Are they any pointers to what arguments I could use with ffmpeg to achieve all this?

I am very new to this fascinating subject and I am keen to learn and understand rather than copy & paste (sounds like a job interview!).

I am using C# as a programming tool but consider this is irrelevant to the initial question?

I very much welcome people's thoughts.

Thanks!

**Tags:** ffmpeg

> **Comments:**
> **username_1**: Old question, but as soon as you started talking about detecting pixel differences yourself, I just had to say:  It's not worth it to invent your own video codec.  Use h.264 or something.  Huge amounts of time have been put into optimizing the hell out of x264, for example.  And use ffmpeg's `mpdecimate` to drop similar frames before sending them to a video codec.  
> **username_0**: @PeterCordes I appreciate your comments. I have to admit when I originally posted this question I was VERY naive.  Now I am just naive :) Thanks  

---

## 3 answers

### Answer by username_2 on 2013-11-19. Score: 1

The way I've seen this done in other systems intended for CCTV is to encode every couple of frames compressed with something like [MJPEG](http://en.wikipedia.org/wiki/Motion_JPEG) and then stream that to the client, the server component of ffmpeg, [ffserver](http://www.ffmpeg.org/ffserver.html) may be able to help you with that.
Of course, depending on your needs a different video codec may be more appropriate, particularly if you intend to send every frame in which case something like H.264 may be more efficient.

> **Comments:**
> **username_0**: Hi, that has given me something to think about -thanks  
> **username_1**: A fixed camera is always going to have a LOT of temporal redundancy between frames, so you'll save a lot of bits with any codec that can use P and B frames (inter prediction).  

---

### Answer by username_1 on 2015-02-26. Score: 1

So you have a client computer with a camera, and want to stream video from it to a server where it's archived and accessible?

client runs:
`ffmpeg -i some_kind_of_input -vf mpdecimate -c:v libx264 -preset medium -crf 25 some_kind_of_output`

Choose an appropriate framerate to capture. mpdecimate will run constantly at that framerate. libx264 with your chosen preset will have to keep up with realtime to avoid dropped frames when there is motion going on, so mpdecimate isn't dropping any duplicate frames.

The output from that should be buffered somehow so upload to the server can lag behind the bitrate of the output when there is a burst of data (because of motion).

Your output will be VFR video. There will be continuous 10fps or w/e bits, and other bits with 1 frame per hour or something. x264 doesn't care, and most container formats have no trouble storing timestamps for each frame. (e.g. mkv or mp4 are probably both ok choices.)

The server receives this video stream, and breaks it into 1 minute or 1 hour chunks or something. (or maybe it's easier to break into chunks on the client side, and upload a chunk after it's done being written. That would stop you from getting a realtime feed from your cam though.)

Or server just decodes it and stores jpg images with timestamps for filenames. I'm not sure what kind of support ffmpeg has for VFR output to the `image2` muxer (that's the "directory of image files" with `%04d.jpg` filenames, or w/e. AFAIK, the `%04d` can only substitute frame numbers, not timestamps.)

Anyway, I just made all this up. I'm sure you'll find lots of stuff if you google on motion detection security video.

IDK why this was on the recent-activity questions list, but since I read it, I figured I'd post an answer.

> **Comments:**
> **username_0**: I too do not know why this has appeared on the recent-activity list but I am glad it did!  If I can work out how this happened I will attempt to regurgitate all my past questions :)  Also, your answer was A1!  
> **username_0**: Hi,  When i run this command via my C# app I just a get byte values of 255?  What have I missed?  
> **username_1**: you need ffmpeg installed, and `some_kind_of_input` is just a placeholder.  Same for output.  If you want to use it from within  a `C#` app, you might want to use the `libav*` API, instead of piping data through ffmpeg running as an external process.  I haven't used the api myself, but `ffmpeg` (the cmdline program) is just a front-end for `libavformat`, `libavfilter`, `libavcodec`, etc.  Have a look at http://www.ffmpeg.org/documentation.html, and google on using ffmpeg libraries.  (also note that there's a fork of ffmpeg called libav, so you'll get google hits on that.)  
> **username_1**: sorry, didn't take as long thinking about your reply as I should have.  So you mean the output file is filled with `0xff` bytes?  Or the `system()` function call returns 255?  I really don't have a very good idea of exactly what you might have done.  You might want to ask a new question (with code and output) on stackoverflow.com (which would be the right place, since you're writing code.)  You might get answers telling you to use the library API instead of trying to pipe your data through the cmdline frontend, though.  If ffmpeg can read directly from the camera, that might be useful...  
> **username_0**: Yes, deleted as I thought it was a reply to another of my questions lol. Using mobile to reply, sorry  
> **username_0**: I prob need to post a new question. If use those command arguments in a c# pipe app I get a stream of 255 bytes. Before I got complete images before app stopped. I will need to get to a PC to use this in a dos windows and capture the standards error  
> **username_0**: Peter Thanks for those links you gave me.  the C# is just medium I can use.  It was notiecable that every raw byte was 255 with those command arguments you kindly supplied.  I need to now try and replicate it on the command line and try to get a log file to be able to submit a new question.  It is still ffmpeg specific as opposed to C# specific and I know the C# code is all ok. so just down to those command arguments really..:)  

---

### Answer by username_3 on 2013-11-19. Score: 0

If you are on Windows (presumably since you are using C#) is there a reason that you can't simply use Windows Media Encoder and Windows Streaming Media Services on the server? It sounds like it is designed to do exactly what you are trying to do. I'm not sure I understand why you need to build your own solution when many great (and even free or no additional cost) solutions exist already.

Update:
Based on the understanding that you just need about 10fps jpeg's and that latency doesn't matter as much as guaranteeing delivery, I would probably do the following:

1) Client submits each image with an incrementing sequence number to the server.

2) The server takes each image and stores it in to the file system with the path and sequence number in a dictionary.

3) When the viewer connects it starts by asking for the current image and is given the image and sequence number.

4) Upon display of an image, the viewer then requests the next sequence number. Timing information for how far behind the viewer is could also be sent back to keep it from getting too far behind. The server would wait until an image is available for the sequence number if it doesn't yet have one. This would help keep in sync as well.

> **Comments:**
> **username_0**: Hi, thanks for replying. I did take a look at Windows media Encoder but it is working with streams.  I am using jpegs you see.  
> **username_3**: @AndrewSimpson - ah, so video (even at low framerate) isn't an option? Would you like to jump in [chat](http://chat.stackexchange.com/rooms/733/audio-video-chat) to discuss a bit more about what you need.  I'm still not 100% sure I understand the scenario.  
> **username_0**: hi, I would love to. How do do that?  
> **username_3**: @AndrewSimpson - updated my comment with a link on the word Chat, just click it. :)  
> **username_1**: ffmpeg has an input demuxer that can read a stream of image files piped in back-to-back.  `-f image2pipe`.  Or just decode the jpegs, and feed the image data to WME.  

---

