3

How to record video and audio in a separate files for every 40ms continuously upto 10 seconds from webcam using FFMPEG?

Thanks.

VijayR
  • 69
  • 1
  • 10
  • try refering this https://stackoverflow.com/questions/6311560/how-can-i-capture-audio-and-video-simultenaous-with-ffmpeg-from-a-linux-usb-capt – Education 4Fun Jan 06 '21 at 14:39
  • Thanks for the reply, how about recording for every 40ms continuously upto 10s? – VijayR Jan 06 '21 at 15:06

1 Answers1

3

Step 1: Webcam Capture

As already mentioned in the comments follow this instructions for audio capturing. How can I capture audio AND video simultenaous with ffmpeg from a linux USB capture device
We start with his:

./ffmpeg -f alsa -i hw:0 -f video4linux2 -i /dev/video0

More details are explained in the FFmpeg documentation.

Step 2: Segmentation

./ffmpeg -f alsa -i hw:0 -f video4linux2 -i /dev/video0 -t 0:10 -segment_time 00:00.040 -f segment out_%003d.mp4
  • -t 0:10 records only for 10 seconds
  • -segment_time 00:00.040 split every 40 milliseconds
  • -f segment use a segmented format
  • out_%03d.mp4 output file name template (%03d is used for a better formatting; 001, 002, 003 and so on)

Again here's the link to the FFmpeg documentation for segmentation.

martinr92
  • 588
  • 1
  • 7
  • 15
  • Thanks for the detailed explanation. Along with above command what I have to add for generating respective aac audio file like out_%3d.aac? And if I want to video format in h264 format should I add libx264 or just name as out_%3d.h264? – VijayR Jan 06 '21 at 16:17
  • FFmpeg tries to detect the codec automatically based on the file extension (so .h264 and .aac should work). You can also explicitly tell FFmpeg to use a specific codec: `-c:v libx264` or `-c:a aac`. – martinr92 Jan 06 '21 at 17:33
  • Ok. So now the command would be ./ffmpeg -f alsa -i hw:0 -c:a aac -t 0:10 -segment_time 00:00.040 -f segment audio_%003d.aac -f video4linux2 -i /dev/video0 -c:v libx264 -t 0:10 -segment_time 00:00.040 -f segment out_%003d.h264 Is this correct? – VijayR Jan 06 '21 at 17:50
  • Have you executed the command? I have no linux system with camera, so I can't test it for you. Typically you define first all inputs and then the processing: `./ffmpeg -f alsa -i hw:0 -f video4linux2 -i /dev/video0 -c:a aac -t 0:10 -segment_time 00:00.040 -f segment audio_%003d.aac -c:v libx264 -t 0:10 -segment_time 00:00.040 -f segment out_%003d.h264` – martinr92 Jan 06 '21 at 18:51
  • I have executed the command, but it only generated 27 audio files and 2 video files. As per the calculation I need to get 25 audio and video files per one second and for 10 seconds 250 audio and video files. – VijayR Jan 07 '21 at 04:59
  • I increased thread queue size to 1024, then 250 audio frames generated as expected, but only one video frame is getting generated. I think this command is executing sequentially first audio and then video, how generate in it parallel? – VijayR Jan 07 '21 at 05:24
  • Any suggestions? – VijayR Jan 07 '21 at 15:35
  • It seems that the segmentation for video needs an key-frame so you can add `-f 25 -g 1` for encoding the video in 25 FPS (because 1000 milliseconds / 40 milliseconds = 25 frames) and tell FFmpeg to use every frame as a key frame (`-g 1`). Alternatively you can tell FFmpeg to ignore this rule with the parameter `-break_non_keyframes 1` or may try the parameter `-write_empty_segments 1` to write also a file, if no data was received from the webcam. The log during execution tells me on my static file that its processed parallel (check the "Opening 'xxx' for writing"). – martinr92 Jan 07 '21 at 16:14
  • **Thank you** @martinr92. Now I'm able to generate audio and video files in parallel, but when I use ffplay on output files it throws following different errors for all files. Audio Error `Format mpegts detected only with low score of 2, misdetection possible! Could not detect TS packet size, defaulting to non-FEC/DVHS` `sample-005.aac: could not find codec parameters` `Format aac detected only with low score of 1, misdetection possible! Estimating duration from bitrate, this may be inaccurate` and video error `frame-006.h264: Invalid data found when processing input=0/0`. kindly help me. – VijayR Jan 08 '21 at 10:17
  • Seems that ffplay is missing metadata for the audio/video. Therefore you need a container that stores metadata and audio/video data like MP4. If you leave the codec configuration as is and just set as file extension `.mp4` instead of `.h264` or `.aac` you should have also metadata for the player. – martinr92 Jan 08 '21 at 11:02
  • Thanks for the suggestion, but I need it particularly in h264 and aac format for uploading into AWS Kinesis Video Streams. Is there any other way to add meta data? – VijayR Jan 08 '21 at 15:00