3

I am trying to record video from the USB WebCam connected to my Windows desktop. Sometimes, I get a mp4 file which I can play using VLC. But sometimes, I can't play mp4 file at all (the file size seems valid video file, 37,528KB).

Here is the command I used:

ffmpeg -rtbufsize 1500M -y -f vfwcap -i 0 output.mp4

I would like to know:

  • why I get a corrupted file sometime with the above command
  • how can I fix the 'corrupted' mp4 file?

I have tried the suggestion from bertieb. I get " moov atom not found .\corrupt.mp4: Invalid data found when processing input"

When I open the mp4 file, this is what I see. I don't see 'moov in the header. enter image description here. Is there other way to recover the video files? And Should I still switch the format from mp4 to mkv for future capture (since the assumption was the recording was killed before it is properly encoded.

1 Answer 1

3

Short version:

  • files are corrupted because you are interrupting to finish the mp4 encode, switch to something like mkv instead.
  • 'uncorrupt' by format shifting to another container ffmpeg -i corrupt.mp4 -c copy not_corrupt.mkv
  • don't use vfwcap as it is out of date

Why am I having problems capturing from my USB webcam with ffmpeg?

From the ffmpeg docs on vfwcap:

Note Well this is out of date, these days you can use the -f dshow device to capture from a directshow device.

Since it is out of date, use -f dshow instead. From the DirectShow documentation:

FFmpeg can take input from "directshow" devices on your Windows computer. See the ​FFmpeg dshow input device documentation for official documentation. It can accept input from audio, video devices, video capture devices, analog tv tuner devices.

From the linked dshow documentation, you should first find out what the device is under dshow:

ffmpeg.exe -list_devices true -f dshow -i dummy

Example output:

[dshow @ 0000000002e9bb20] DirectShow video devices (some may be both video and audio devices)
[dshow @ 0000000002e9bb20]  "PC Camera"
[dshow @ 0000000002e9bb20]     Alternative name "@device_pnp_\\?\usb#vid_058f&pid_3880&mi_00#6&218cd81a&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"

using the "PC Camera" found by the previous command:

ffmpeg -f dshow -i video="PC Camera" -y output.mkv

(I would back off on using -rtbufsize initially, as there are some caveats noted for using it with dshow devices`)

I would suggest using mkv over mp4 as my guess is you are not doing a fixed-time encode but instead terminating ffmpeg with Ctrl-C; this prevents ffmpeg from writing certain atoms to the video container and likely why the file is corrupted1. mkv can be interrupted at will2.


1: You could test this by format-shifting the corrupted mp4s: ffmpeg -i corrupt.mp4 -c copy not_corrupt.mkv. If that doesn't work, another answer on SU by sparrowt recommends untrunc

2: Tested using ffmpeg version N-72939-g5b0f55a on Windows, and a random cheap webcam I happened to have lying around

3
  • 1
    Thanks for the detailed answers. I get this error when I try to format shifting " moov atom not found .\corrupt.mp4: Invalid data found when processing input"
    – n179911
    Sep 11, 2015 at 17:04
  • Sounds like the files are indeed truncated (from Ctrl+C or however), another answer on here recommends untrunc; which I will edit into the body of the answer if that works for you
    – bertieb
    Sep 11, 2015 at 19:33
  • mkv doesn't seem to be tolerant of unexpected stops either... i tried using ffmpeg to record to mkv and killed it during the process and the mkv file was also unplayable. what format should i record directly to so it won't be corrupted if the process is killed or interrupted?
    – Michael
    Apr 10, 2020 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .