I'm copying a video from a camera stream using a command cat /dev/video0 > file.mpeg This works perfectly, however I want too break up the file into smaller chunks... So I've been waiting pressing Ctrl+C.. Wanted to automate that so created a batch script that will wait X minutes then halt the job, meanwhile show a progress bar. Works great however I want to take it another step.. I have an XML file that contains the exact times and file names, I have a mono program that parses this file.

Here's the question.. How can I programmatically run this cat command. Sure I could execute a system command. But I was thinking of just doing the entire application in the same language. I can open a file to write as a binary write and read say 1000 bytes at a time... But what is cat doing? Does it know how much/ how fast to read somehow?

closed as too broad by Rui F Ribeiro, Vlastimil, msp9011, Mr Shunz, Archemar Jan 23 at 15:03

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    I think and I might be wrong, that the cat reads and outputs the file as fast as the source device, system and CPU allows. – Vlastimil Jan 19 at 4:01
  • 1
    You could use dd to limit how much data is written from the device to stdout or a file. – Peschke Jan 19 at 9:04

Let timeout kill cat

Since it works well enough with cat and ctrl+c, the following command line should work with data according to the question and assuming 5 seconds play-time to be recorded.

timeout 5 cat /dev/video0 > file.mpeg

cat is a simple tool for reading/writing data. There is no way to set the speed or time interval for the data transfer in cat. You have to control it outside cat. But there are many other tools.

Use the built-in features in ffmpeg

It is also possible to use the multimedia tool ffmpeg. There is a steep learning curve, but when you get into using it, you will find that it is a very powerful tool. There are detailed instructions in the manual man ffmpeg but it is better to search for a suitable tutorial via the internet.

I have tested the following command line with conversion from an MTS file to an mpeg file. I have not tested it from a device, but I think it will work. You may want to control the conversion (to be different from the defaults) using more options in the command line.

ffmpeg -t 5 -i /dev/video0 file.mpeg

where -t 5 means that 5 seconds are recorded.

The following command line may be an important improvement,

ffmpeg -i /dev/video0 -f segment -segment_time 10:0 out%03d.mp4

It will split the input into files of 10 minutes length. you should look at the docs here


  • In my working computer with Lubuntu 18.04.1 LTS, there is ffmpeg version 3.4.4-0ubuntu0.18.04.1, so version 3.4.4. This is what you get if you install from Ubuntu's repositories. Other linux distros and versions have probably other versions (newer or older).

  • The link (to https://ffmpeg.org) describes version 4.1, and some features in this version may not be there in the older version. This version is more advanced, but may be more complicated to get and to keep up to date with bugfixes.

  • @user1529413, The community is downvoting this answer. If you find it useful, please let me know. Otherwise I will delete it (after some time for you to test it). – sudodus Jan 19 at 6:44
  • 1
    Take a look at the timeout command, as in timeout $ptime cat ..., which seems to do pretty much everything your script does. (Not entirely sure why you're being downvoted, other than the OP asked for code in mono to replace cat..., which is a low-research question in itself.) – roaima Jan 19 at 10:12
  • @roaima, Thanks for teaching about timeout :-) I replaced the separate 'sleep' and 'kill' commands with 'timeout', and the shellscript with a oneliner. Now let us wait for the response from the OP. – sudodus Jan 19 at 10:53
  • 1
    What about improving instead of deleting it? using ffmpeg is the way to go: ffmpeg -i /dev/video0 -f segment -segment_time 10:0 out%03d.mp4 will split the input into files of 10 minutes length. you should look at the docs here. – pizdelect Jan 19 at 13:38
  • 1
    This is very helpful information, it annoys me to no end that's it's been down voted before I can even see it. Stack overflow has a major problem it needs to address, you've created a system that chases away people. This comment no doubt will be down voted as well. – user1529413 Jan 19 at 14:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.