7

I'm using ffmpeg from the command line to capture from a webcam to a file using the following:

ffmpeg -y -rtbufsize 702000k -f dshow -s 320x240 -r 25 -i video="<device name>" -t 10 -vcodec mjpeg -q:v 2 out.mp4

There is a slight delay between executing the command and the start of the capture (~0.5 sec). I'm trying to find a way to accurately determine the start time (UTC/GMT) of the capture.

My initial thought was to use the file-creation time as this might accurately reflect when the first frame was encoded (as opposed to when the command was executed). Unfortunately the file creation time is only accurate to the second which is not precise enough (and I'm not sure this would have given an accurate result anyway).

My next thought was to use ffmpegs timestamp option. According to the documentation (http://www.ffmpeg.org/ffmpeg.html):

‘-timestamp time (output)’

Set the recording timestamp in the container. The syntax for time is:

now|([(YYYY-MM-DD|YYYYMMDD)[T|t| ]]((HH:MM:SS[.m...])|(HHMMSS[.m...]))[Z|z])

If the value is "now" it takes the current time. Time is local time unless ’Z’ or ’z’ is appended, in which case it is interpreted as UTC. If the year-month-day part is not specified it takes the current year-month-day.

So I added the option:

ffmpeg -y -rtbufsize 702000k -f dshow -s 320x240 -r 25 -i video="<device name>" -t 10 -vcodec mjpeg -q:v 2 -timestamp now out.mp4

Unfortunately ffmpeg doesn't seem to like this:

Option timestamp (set the recording timestamp ('now' to set the current time)) cannot be applied to output file out.mp4 -- you are trying to apply an input option to an output file or vice versa. Move this option before the file it belongs to.

Error parsing options for output file out.mp4.

Error opening output files: Error number -22 occurred

The documentation says -timestamp is an output option and it appears to be applied to the output file so I'm confused by this error.

Can anyone suggest a way to accurately determine the capture start time?

Does anyone know why the -timestamp option gives an error?

  • So you're trying to figure out the wall clock time of the first frame? You could add the timestamp to the frames as an overlay I suppose... http://stackoverflow.com/q/12293853/32453 may be related – rogerdpack Jan 21 '14 at 17:58
  • @rogerdpack 'So you're trying to figure out the wall clock time of the first frame?' - Yes, exactly - accurate to a few frames. The command is being called from a script which needs to know this time for subsequent processing. Overlaying the time on the image doesn't help since the script cannot use this. –  Jan 22 '14 at 08:59
  • not much comes to mind, though you could possibly add the "showinfo" filter and then poll to see when the first frame is added to its output file...I'd probably suggest re-asking on the FFmpeg mailing list... – rogerdpack Jan 22 '14 at 16:39
  • when FFmpeg first starts it spits out stream info like "start time" maybe use that... – rogerdpack Jan 01 '15 at 16:37
  • Hi @Adam. I am trying to achieve the something similar: I record audio and would like to have timestamps, at least of the beginning. Did you by any chance succeed? – XonqNopp Dec 15 '17 at 15:28
  • See https://ffmpeg-user.ffmpeg.narkive.com/fzTrnfHX/getting-precise-start-time-from-wall-clock-for-capture – rogerdpack Apr 17 '20 at 22:51

1 Answers1

-1

Set your PC OS time to display as like YYYYmmdd time like hhmmss then it is easier to grab the time in a batch file and assign it to the first frame then you use frame numbers divided by frame rate to add for each frame to the original time stamp. This is the only way I have been able to get a running account to display the time in a video.

 REM ## GETTIME ##
 SET DTG1=Reserved
 SET DT1=%DATE%%TIME%
 IF "%DT1:~8,1%" EQU " " (SET DTG1=%DT1:~0,8%_0%DT1:~9,5%UTC) ELSE (SET DTG1=%DT1:~0,8%_%DT1:~8,6%UTC)
 REM ## END GETTIME ##

 @START /I /MIN /REALTIME c:\ff\FFmpeg.exe -y -loglevel error -rtbufsize 2.0e+009 -f dshow -r:v 10.000 -s 1920x1080 -vcodec mjpeg -i "video=Logitech HD Pro Webcam C920" -vf "fps=10.000,drawtext=fontfile=ocra.ttf:text=%DTG1%+%%{n}/10sec:fontsize=24:fontcolor=orange:x=999:y=19:box=0:boxcolor=black" -q 0 -f image2 B:\(%%d).jpg 1> nul 2> nul
 @TIMEOUT /T 3 /NOBREAK 1> nul 2> nul
 @START /I /MIN /REALTIME /W FFmpeg.exe -y -loglevel error -f dshow -ac 2 -r:a 44100 -i "audio=Line In (VIA HD Audio)" -f s16le -fs 63504000 B:\a.raw 1> nul 2> nul
 @TASKKILL /F /IM FFmpeg.exe /T 1> nul 2> nul
 @TASKKILL /F /IM FFmpeg.exe /T 1> nul 2> nul

At least this is how I do it.

Krishna Mohan
  • 1,503
  • 3
  • 22
  • 28