26

How can I keep the flow (protocol rtsp, codec h264) in file (container mp4)? That is, on inputting an endless stream (with CCTV camera), and the output files in mp4 format size of 5-10 minutes of recording time.

OS: debian, ubuntu Software: vlc, ffmpeg (avconv)

Currently this scheme is used:

cvlc rtsp://admin:admin@10.1.1.1:554/ch1-s1 --sout=file/ts:stream.ts
ffmpeg -i stream.ts -vcodec copy -f mp4 stream.mp4

But it can not record video continuously (between restarts vlc loses about 10 seconds of live video).

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Ruslan Sharipov
  • 703
  • 2
  • 7
  • 10

3 Answers3

25

See this question and answer on Server Fault. ffmpeg will do what you want.

The feature you are looking for is called segmentation. Your command line would look something like this:

ffmpeg -i rtsp://10.2.2.19/live/ch01_0 -c copy -map 0 -f segment -segment_time 300 -segment_format mp4 "capture-%03d.mp4"


EDIT: Answer originally used 'avconv' instead of 'ffmpeg', which made sense at the time (distributions were dropping ffmpeg for avconv) but no longer does. Changed it to use ffmpeg instead.

Alexander Garden
  • 4,468
  • 3
  • 31
  • 25
  • 5
    I would advise that you want to use `-segment_atclocktime 1` if the application is for "CCTV". As this will try to split based on the wall clock and not time since recording began. – Aron Nov 23 '15 at 14:54
  • 9
    Ffmpeg did not become avconv and avconv is not the new ffmpeg. They are different projects. https://stackoverflow.com/questions/9477115/what-are-the-differences-and-similarities-between-ffmpeg-libav-and-avconv – Cristian Toma Sep 13 '17 at 05:13
  • Linked question has a top-voted, accepted answer which uses ffmpeg. Downvoted because it uselessly promptes `avconv`. – Arthur Attout Apr 30 '23 at 22:18
  • 1
    Thanks, @ArthurAttout. Updated the answer. Things have changed since it was originally written. – Alexander Garden May 05 '23 at 04:20
20

Alexander Garden solution works for ffmpep using the version below. Replace avconv with ffmpeg.

./ffmpeg -i rtsp://10.2.2.19/live/ch01_0 -c copy -map 0 -f segment -segment_time 300 -segment_format mp4 "capture-%03d.mp4"

I'm including this header because of the FFmpeg confusion over versions, the ubuntu schism and rapid development.

ffmpeg version N-80023-gd55568d Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.1)
  configuration: --prefix=/home/rhinchley/q10/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/home/rhinchley/q10/ffmpeg_build/include --extra-ldflags=-L/home/rhinchley/q10/ffmpeg_build/lib --bindir=/home/rhinchley/q10/bin --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree
  libavutil      55. 24.100 / 55. 24.100
  libavcodec     57. 42.100 / 57. 42.100
  libavformat    57. 36.100 / 57. 36.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 45.100 /  6. 45.100
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Ron Hinchley
  • 299
  • 3
  • 5
  • 1
    Thanks for sharing this version of FFmpeg's solution, I'd prefer to use FFmpeg over avconv. And it would be better to wrap up your URL with double-quotes so that bash won't be confused with strange characters which will cause you stuck at FFmpeg's header section. – AJ H Oct 04 '17 at 07:08
-2

Team work: Split the video source and have two processes alternate recording the time frame. You'll want to test how variable the startup time is, and how variable it is. You might want to set the processes priority to realtime to reduce start time variance. There will be some overlap but that sound like it might be ok for your application from what I infer. Example:

p1: sRRRRRRRRRwwwwwwwwsRRRRRRRRRwwwwwwwwsRRRRRRRRR...
p2: wwwwwwwwwsRRRRRRRRRwwwwwwwwsRRRRRRRRRwwwwwwwww...

                    time -->

s: startup
R: running
w: wait
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156