I took some side photos of my pregnant wife, hoping to make a time-lapse, but I never got around to it. The photos aren’t perfectly aligned since her position changes a bit in each one. Can ffmpeg fix that?
$ ffmpeg-english "capture video from the camera every 1 second and write it to jpg files"
$ ffmpeg-english "take all of the images ending with .jpg in this directory and make a 30fps timelapse of it"
Sound awesome? Here's the code for ffmpeg-english:
#!/usr/bin/env python3
import openai
import sys
import os
import time
# Ensure you have set your OpenAI API key as an environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
client = openai.OpenAI()
def get_ffmpeg_command(task_description):
# Call the OpenAI API with the task description
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role":"system", "content":"You are an expert in FFmpeg commands. Given the following English description of a task, you respond with the correct FFmpeg command. Do not include any other information in your response except for the command only."},
{"role":"user", "content":task_description},
],
max_tokens=150,
temperature=0.5
)
command = response.choices[0].message.content.strip()
return command
def main():
if len(sys.argv) < 2:
print("Usage: ffmpeg-english <task_description>", file=sys.stderr)
sys.exit(1)
task_description = " ".join(sys.argv[1:])
ffmpeg_command = get_ffmpeg_command(task_description)
# some basic guardrails
assert(ffmpeg_command.startswith("ffmpeg"))
assert(";" not in ffmpeg_command)
assert("|" not in ffmpeg_command)
print(f"Executing command: {ffmpeg_command} in 2 seconds (^C to cancel)")
time.sleep(2)
os.system(ffmpeg_command)
if __name__ == "__main__":
main()
I took photos every few minutes for about a year. I wrote some custom code to pick out a set of frames from several consecutive days, but with the sun at the same angle in the sky. I then blended these frames together. This makes a ghostly look, without the bright flashes of cloud passing or weather changing. As I moved forward in time, I selected frames from the next angle in the sky, so it looks like only a day passes. You’ll see the shadows move as if a day passes, when really a year goes by.
Very cool! We tried to do the same thing with a series of photos taken along the solar eclipse, but didn't have enough density or consistency to make it compelling.
I use a Raspberry Pi Zero with a Pi Camera and ffmpeg to make timelapses of plants growing from seedlings each season when we plant tomatoes/potatoes/etc. It's a great way to show the kids how different plants grow from seed, and such a cheap/easy project.
Bash script running on a cron job every hour with:
DATE=$(date +"%Y%m%d%H%M")
raspistill -o /home/pi/timelapse/$DATE.jpg -awb off -awbg 1.8,1.6
Been making timelapses with ffmpeg since forever, such a great tool. I try to always have a cam pointed at the sky and upload a snapshot every minute. A telegram command triggers the creation of a timelapse with a similar cli command like the OP. https://www.youtube.com/watch?v=5GvaFBzOu2c
I use an old Android phone with a long deprecated app called MobileWebCam (still available on certain sites) I removed the battery and connected the charger directly. It uploads a picture every minute, a Node.js backend creates the timelapse with ffmpeg. Currently experimenting with a TP-Link C520 cam and a Raspberry Pi. You can point the camera to different positions using ONVIF, use ffmpeg to grab the stream and take a snapshot, then process this again on the server. Downside is the wide angle / fish eye lens and occasionally a corrupted stream snapshot.
If you want an all sky solution, zwoastro.com ships an all sky lens with many of their small sensor planetary cameras. There's also a selection of software to handle the photos, making time-lapse, uploading etc. It came in really useful for seeing the Aurora outburst. Here's a short write up I did for my local club recently: https://m.facebook.com/story.php?id=101640086556073&story_fb...
I'm also working on a new one that uses an ASI533 Pro and a 4mm lens from a mirrorless camera to get better quality images.
Nice, thanks! I tried using ffmpeg for a minor video editing task I had a few months ago - just a cut, crop, rescale, and volume adjust. I've tried a few of the mainstream GUI video editing tools, and IMO, they all have incomprehensible UIs, are way too bloated, and usually far too expensive for what I'm trying to do. FFmpeg may not be dead simple, but I find it much easier to skim the command line flag list to figure out how to do what I want. And once I do, I can save down a handful of useful sets of flags and refer to them next time. Cheers to ffmpeg, one of the kings of FOSS! If you ever feel the need to do any kind of video conversion or editing, definitely try to do it in ffmpeg first.
I recently found LosslessCut (https://github.com/mifi/lossless-cut) that is basically a GUI for ffmpeg, you can make simple edits without re-encoding the stream.
Indeed. I find it baffling how hard it is to just make lightweight edits to videos on Windows. At a bare minimum I would like to clip a video, or crop or change audio tracks. My cheat sheet of ffmpeg commands still remains to be the easiest way for me to do this.
There are a lot of free apps to make lightweight edits to videos on windows. One example is Avidemux. I have high-end video editors but Avidemux comes in handy more often than it should.
Davinci Resolve has a free (as in beer) version that is quite capable and easy to use, even as someone who'd only used iMovie before. The only problem is that "how to do X in Davinci Resolve" has been taken over by slop.
Davinci Resolve is actually the first thing that came to mind on the subject of, okay it's free, that's nice, but I can't for the life of me figure out how to do anything in it. I suppose it's not necessarily their fault that the search results for how to do basic things are garbage, but I guess an advantage of CLI apps is how-to results for them don't seem to attract nearly as much SEOified clickbait.
I shoot RAW from an older Canon 5D which Resolve does not read natively. So there's a bit of a conversion step going from CR2. My typical workflow is to use Adobe RAW to process the images, then import the RAW directly to AE to render out with whatever repositioning or cropping. Let's not forget LR Timelapse[0] as part of the workflow too.
I was reading about how to create timelapses on a canon dslr and someone pointed out that each frame you take adds to your shutter count and consumes the lifetime of your shutter. Do you find that a problem? For example a frame every 2 seconds for 8 hours is 14,400 actuations of the shutter.
I worry about my shutter count like I worry about my HN points...I don't. If the shutter was to fail from "too many" clicks, it can replaced. It's not the end of the world. There are people that will point out that you can die every time you drive your car, but does that prevent you from driving your car? I easily have over 150k (I don't have the right cable/adapters available at the moment to confirm the number USB-C to USB-mini) to get actual number. Even if the shutter were to fail, I'd just have it replaced for less than the price of a new body.
To be fair, pulling out a professional video editor for small changes is like learning emacs to edit some config files. You don't need 99% of the features.
Also as an FYI to everyone, FFmpeg does support nVidia GPU acceleration but it might not be enabled in your build. So check if you use it a lot.
Probably true, but ffmpeg seems to have a ton of features too. It seems to me that CLI apps are inherently better at not distracting you with things you don't need. A CLI flag that you don't use is invisible outside of the man pages, not so for a menu or toolbar of a zillion options with names and icons you don't understand.
I found https://github.com/mifi/editly to be an intuitive frontend for this type of task - I used it to create a montage of several clips and was able to easily adjust parameters around timestamps and such to get the montage perfect
Be aware that -pattern_type glob is not supported on Windows, though, iirc. A workaround is to name your jpegs with consecutive numbers (not necessarily starting at 0) and use a pattern with a counter placeholder in it instead.
Or you can use '-f concat' and specify a text file with the explicit order of image files to be used as input - no need to hope and pray that the wildcard will pick the files in the correct order.
Note of support: ffmpeg supported many of the transcoding needs of my former employer back in 2007, being a "friendly" tool to the team. Yes it had/s issue. Being open source gave us a lifeline, to be able to fix our own stuff, and build up our video and audio live streaming and video watching white label service.
I recently wrote a blog post about doing this to create timelapses of Rimworld colonies. I didn’t realize -pattern_type glob didn’t work on windows though… I’ll have to update it.
Also, an assumption in your command is that all the images are the same aspect ratio. If they’re not, you can use this to dynamically pad it out with black bars on either size:
Seems like there are a lot of experts here with FFmpeg, so perhaps one of you can help me.
I have a bunch of old videos that use MTS as the file extension. I would like to convert these to an MP4 (or MKV) file but I would like to do it while keeping the metadata (date taken, date created, etc.). Is there a way to do this?
The scripts I've seen change the file extension by just changing the container but they never keep the metadata which is a huge issue for me.
ffmpeg works really well for this. I have also been making time-lapse movies for over a decade, starting with using just Quicktime Pro 7 to combine the images into movies. Later using various other tools. Now I also use ffmpeg, and want to keep track of exactly how each movie is put together so looked for a way to script each movie. Now I use ffmpeg-python (not a lot of recent updates, but just works) to steer ffmpeg, with my own time-lapse specific Python package https://pypi.org/project/time-lapse/ to assemble the movies, and for each movie I have a single script which describes which frames are part of it and how they are combined into a movie; https://github.com/153957/time-lapse-scripts
I am quite happy with the setup, making it easy to recreate movies in the future from source, but at higher resolutions or different codecs.
I looked into using ffmpeg to “compress” video podcasts by lowering the framerate a lot, but it didn’t seem to do as much as I thought (about 50% size reduction). The theory was that a video podcast is mostly talking heads with an occasional chart on the screen, so you really only need a frame every second, or five seconds.
Reducing framerate doesn't help much when there isn't a lot changing between frames. Here are some better optimizations:
Noise reduction, so you compress less useless noise: -vf hqdn3d
Turn up the Constant Rate Factor. This will make better visual tradeoffs than decreasing frame rate. 23 is a good starting point for h264, but keep increasing it until you don't like how the content looks: -crf 23
FFMpeg really helped me out not too long ago. I tried KDenLive and ShotCut to edit some videos, which I rarely do, only to be overwhelmed and then discover that ffmpeg command can do everything from timelapses to trimming and brightness/contrast adjustments. And you can "preview" the result too, using ffplay.
I usually open them up in a new project just to create a lossless input video to work with in After Effects, and use that (if I use image sequence directly, DaVinci Resolve acts in weird ways).
Right, calling it technically lossless is wrong but 422 HQ gives impressive results so I we can probably safely say that it is "practically" lossless.
In DaVinci Resolve, many tools go awry with image sequences: for example temporal noise reduction simply doesn't work with a compound clip with image sequences, I also remember having problems with caching performance. I have a few very strange/buggy problems with Resolve though I love color grading with it regardless, so I want to avoid the buggy sides of Resolve (as it's one of the buggiest software I've ever used) but use the upsides of it (grading and some OpenFX filters).
00:01:00 is where to start the flip book, 30 is thirty seconds worth, and 1 fps is how many frames per second. this'll make output_XX.jpg from the Avi which you can then print
I'd recommend Da Vinci Resolve for making timelapses. It performs really well and let's you scrub through before rendering anything which lets you clip just the part that you need. Plus you get the benefit of high export quality which can be fiddly with ffmpeg.
I would argue that ffmpeg has the hardest to understand interface in the video editing space (unless you can find a command someone else came up with that does exactly what you want)
there are a bunch of flags to get exactly right in order to get it to give you a high quality image out. there are wrappers to do this more easily for you, ffmpeg is a low level tool.
back when computers were hard, tips like this were gold. but these days,
for for a well trod/documented thing like ffmpeg,
asking ChatGPT to make the ffmpeg command you want works really well, eg "give me ffmpeg to make a video from a series of jpegs" and iterate from there.
Please don't let this line of thinking put you (the reader) off sharing tips. Here we now have a thread containing other information we may not have thought to ask anyone/thing about, discussion, history, etc.
teach to fish vs giving of fish, I suppose. I did not mean to discourage or stifle discussion, but point out that there's a tool that makes it much easier to get the right incantation of ffmpeg.
That's awesome! FFmpeg is super powerful. I've been using it for video edits too. It’s amazing how such a simple command can handle complex tasks. Thanks for sharing this tip!
Unfortunately not all versions have "vidstab".
ffmpeg -i "$1" -vf vidstabdetect=shakiness=5:show=1 dummy.avi
ffmpeg -i "$1" -vf yadif, format=yuv420p, vidstabtransform=zoom=2:optzoom=0:crop=black -c:v libx264 -b:a 32k stabilized264.mp4
Yesterweek's shaky video shot from a kayak: https://youtu.be/4pM0VeH4NE0?si=H2qTJfcvis3QmFlj