Image to Video to Image using FFMpeg

Image to Video to Image using FFMpeg

In this post, I’ll demonstrate how to take a series of images and convert them into a video. I’ll also demonstrate how to convert a video into a series of images, were we can do some interesting things. Lets start with image to video conversion.

For this example I’ll be using images that have been scrapped from the web for the past year. I run a script that downloads a new image every 5 minutes. As it downloads them, the script renames the file with a timestamp in the name. I don’t want to use all of the images because the resulting video would have an effect as day and night cycle. I use this command to export all of my images that were taken mid day.

ls /WebScraped/Nasa/IonGlobalMap_*12000*.gif > /WebScraped/Nasa/IonGlobalMap_Noon.txt

Now I have a text file that lists all of the images with “12000” in the name. It does have some impurities. My list also includes times that aren’t exactly at noon. For those, I visually modify the text file. There are better automated ways to do this but my list was short and the time to create that automation was more effort than it was worth. Once my list is correct, I then do some text substitution to get my text file formatted so FFMpeg can process it. To do that I use this command.

sed "s/IonGlobalMap_20/file \'IonGlobalMap_20/" IonGlobalMap_Noon.txt > IonGlobalMap_Noon_Input.txt
sed "s/gif/gif\'/" IonGlobalMap_Noon_Input.txt > IonGlobalMap_Noon.txt
rm IonGlobalMap_Noon_Input.txt

FFMpeg is now ready to use the text file as a reference for what images to take and convert into a video. Here is the command I use to make the video.

ffmpeg -y -r 30 -f concat -safe 0 -i "IonGlobalMap_Noon.txt" -c:v libx264 -vf "fps=30,format=yuv420p" "IonGlobalMap_Noon.mp4"

Here is the resulting video that shows Ion intensity at noon Pacific time for the past year.

Now lets take a video and convert it to a series of images. For this example I’ll be using a video from Youtube.

As you can see, one of the pilots draws everyone’s attention while another does a quick and close pass. Just how fast was the second pilot? To find out that, we’ll need to take what we know about the plane and what we observe in each frame from the video.

Before going further, I should state my disclaimer. This is copyright material and my use is merely for educational and amusement purposes without any profit. Next, the procedures I’m using are simplistic and linear, my results will be wrong because assumptions are made about what is observed. The angle of motion as well as variable frame rate of the source video will produce false values. I know this because the end result value clearly could not be true. Anyway, it was one way how to use images extracted from video. Now back to the process.

First we’ll need to download the video from Youtube to let us process it with FFMpeg. To do that, run this command.

youtube-dl <url of youtube video>

Now we want to take that video and place it in a folder for processing. Using FFMpeg, extract all frames and place these images in that folder.

ffmpeg -i "Blue Angels Fly By.mp4" -vf fps=30 "Blue Angels Fly By %d.png"

Now we’ll have a series of images, 995 in all. There are 2 images that we’ll use as a reference to determine the speed of the second pilot. These are image 322 and image 323. Using GIMP, open these files and make a selection of the second plane from nose tip to tail fin. In the Tool Option, the selection area dimensions should be shown.

In mine, image 322 had a value of 500 pixels, while image 323 had a value of 470 pixels. We know the specs of the plane, a McDonnell Douglas F/A-18 Hornet. It has a length of 56′ 1″, or 1177 inches. Now we can convert our pixels to inches and average them to get 2.43 inches per pixel.

Next we layer both images and make one slightly transparent so that both are visible. With that, we then make another selection from nose tip to nose tip. Using the number of pixels multiplied by our 2.43 inch value we get 485.8 inches of travel between each frame. Again, the assumption is we have a perfectly timed frame rate of 30 per second. Here we have 485.8 inches of travel in .033 seconds, or 828 mph.

What, how can that be?! That would be faster than the speed of sound, I doubt it! Here are the reasons why. The second pilot is flying in an arc and away from the camera. This can be proven because the pixel size of the plane is smaller in the next frame. Another reason is the camera is moving, we clearly see the camera change direction between each frame, the contrails change location. Without a set reference, measuring between two points from both frames is just wrong. One other factor is that the camera, youtube, and ultimately the frame rate from our conversion using youtube-dl is not absolute. We really don’t know if the time between frames was in fact .033 seconds, that value is assumed. When we put it all together, it doesn’t add up properly. So all we can say with certainty is the plane moved really fast.

I hope you have enjoyed this segment about FFMpeg. There is plenty more that it can do, which I hope to cover more of in future posts. Until then, keep looking up.



Comments are closed.