As far as I can tell from the ffmpeg docs, they don't provide a way to use timestamps in the filename rather than sequential numbers. What they provide allows you to create 0001.jpg, 0002.jpg, 0003.jpg but not 2011-08-01 7:30:00.jpg, 2011-08-01 7:30:01.jpg, 2011-08-01 7:30:02.jpg. Can anyone think of a solution that could take the sequentially named files from ffmpeg and name them using their created time?
-
See also stackoverflow.com/q/8544816/32453– rogerdpackCommented Aug 31, 2016 at 17:48
-
Possible duplicate of How to add the timestamp as a part of the generated image file name in ffmpeg– LegolasCommented Dec 25, 2018 at 8:12
-
You can append the timestamps as file names, see this– LegolasCommented Dec 25, 2018 at 8:15
3 Answers
At least the newer versions of ffmpeg
have the strftime
option that allow you to do what you want.
For example:
ffmpeg -f v4l2 -r 1 -i /dev/video0 -f image2 -strftime 1 "%Y-%m-%d_%H-%M-%S.jpg"
Will generate files such as:
2015-11-20_16-25-06.jpg
On unixoid systems, you can use find + ImageMagick to identify each file and its creation date-time. ImageMagick normally has to be installed:
find -name "*.jpg" -execdir identify -verbose {} ";" -ls | egrep "(create|^Image)"
Image: ./logo-text-u-sinn.4.2.do.jpg
date:create: 2011-08-03T12:16:55+02:00
Image: ./tasten-in-tasse_3919.jpg
date:create: 2011-06-14T22:37:07+02:00
gnu:find is available for Windows too, but not normally distributed by Microsoft. Instead, Microsoft has its own, inferior (afaik) kind of find(.exe), with very different syntax.
So if you install gnu-find, it has to be called with the whole path, or renamed to gfind for example, to prevent name collision.
I would write a small script
#!/bin/bash
name=$1
dt=$(identify -verbose $name grep "create:")
datetime=${dt/*create:/}
mv $name $datetime.jpg
which is vulnerable to filenames, including blanks, but if your files are always named 0001.jpg and so on, it will be no problem.
The script would produce 2011-08-03T12:39:42+02:00
for example as a filename, and could be called by find:
find -name "*.jpg" -execdir ./myscript.sh {} ";"
If you don't use/have a bash, you need to modify it accordingly.
Warning:
You have to be careful. If the time of 2 or more files is identical, they will be silently overriden. Of course you may build a check into your script, and maybe your filesystem can store the date more precise than in seconds-interval.
-
I don't think you need to run identify. You can just use the creation date of the file. Alternatively you could just search the point where the filename is generated and exchange with the current time.– whoplispCommented Aug 3, 2011 at 20:25
-
If the files haven't been moved without preserving their attributes, yes. Commented Aug 4, 2011 at 1:40
I ran this grep on the source:
ffmpeg$ fgrep -r snprintf *|grep name
...
ffmpeg.c: snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
...
There is definitely the code to generate filenames with dates. You could try to run your command with the option -vstats. Perhaps it will generate a log file for each output file.
Unfortunately the code is quite complicated. I can't see right now, where the JPEG filename is created. If you have the program running its straight forward use 'strace' but I'm too lazy for that.
-
I ran the vstats option, it created a log file of each frame it processed, but no help with my need: pastie.textmate.org/private/372wq10ebsuqhlrqhg– weotchCommented Aug 3, 2011 at 22:37