Using AI To Create A Video From Traffic Camera Images

Blog

Calgary Traffic Cameras Script For Making Timelapse Video

Using AI To Create A Video From Traffic Camera Images

TLDR: I used AI to write a script that downloads images from a traffic camera then used FFMPEG to combine them into a timelapse video. Fun!

Calgary has a network of traffic cameras that have been placed in strategic locations spread throughout the city, which anyone can view 24/7. As the name suggests, they’re primarily used to monitor traffic, but I typically use them for viewing the weather. In any case, I had an idea.

The way the Calgary traffic cameras work is by recording a series of images every so often and posting them on a website in the format of a short slideshow people can click on. The traffic cameras do not record video, or, at least what’s publicly accessible is not video (I’m imagining privacy concerns?). My idea was to create a timelapse of the images they share, which I thought might look kind of neat. Unfortunately though, I didn’t exactly know how to do it.

I could simply right click and ‘save as’ images the images, but that would be time consuming and inefficient. I would probably miss several images, and, I don’t really have the patience required to save enough images in order to make a decent looking timelapse. I’m sure Linux could do it but I run Windows, and I’m no coder. I spent some time Googling how to do this but didn’t come up with much. There are some Windows based programs that are meant to simulate Linux tools (like curl) that could do it, and I tried some of them but I just couldn’t figure them out – and some weren’t free.

Asking AI for help

I decided to ask AI for help with building a script in Windows PowerShell to automate the task. I used ChatGPT, Microsoft’s Copilot, and phind.

I spent a few days going through some trial and error with the script but in a nutshell this is what I requested:

“I’m using Windows 11 with Windows PowerShell. I want to download an image over and over, indefinitely, every 70 seconds. The images need to be saved in a sequence (1.jpg, 2.jpg, 3.jpg, etc). If a duplicate filename exists, add a number to the file name. I want to save the images to E:\Test”

The various AI’s gave me outputs which I used to put together the script below. There might be more efficient ways of completing this task, I’m not sure, but this has worked for me. I can’t code but I can tweak and modify.

PowerShell script

$urls = @(
    "https://trafficcam.calgary.ca/loc17.jpg"
)

$outputDir = "E:\Test1"


$counter = 1

while ($true) {
    foreach ($url in $urls) {
        $outputFile = Join-Path -Path $outputDir -ChildPath "${counter}.jpg"
        
        while (Test-Path $outputFile) {
            $counter++
            $outputFile = Join-Path -Path $outputDir -ChildPath "${counter}.jpg"
        }
        
        Invoke-WebRequest -Uri $url -OutFile $outputFile
        Write-Host "Image saved as $outputFile"
        $counter++
    }
    
    Start-Sleep -Seconds 70
}

How to use the script

  1. Open Windows PowerShell and copy/paste in the code
  2. Change the image URL to the ones you’re going to use
  3. Change “E:\Test” to a folder on your computer
  4. Save the script
  5. Hit Run

PowerShell will run the script and show you the results in the bottom part of the program. It will run forever, or until you hit stop.

If for some reason you get errors, you can share them with one of the AI’s and ask for help correcting them. They can sometimes make mistakes but they’re pretty good at figuring them out and correcting them.

As well, occasionally an image will not save properly, which I assume is due to the traffic cameras having problems and not updating the image on the website. If this happens, you may miss files in the sequence. You will need to fix these before moving on to FFMPEG to create the video.

For example if you have: 230.jpg, 231.jpg, 234.jpg, 235.jpg (missing 232 and 233) I would suggest copying 231.jpg and renaming it 232.jpg, and copying 234.jpg and renaming it to 233.jpg to fill in the missing slides.

Using FFMPEG

FFMPEG is a popular free open-source program that is used with the Windows command prompt. You need to give it a script with the parameters of the video you want to create. You can download it here: https://ffmpeg.org

Rather than read through the documents and Googling it, I also asked AI for help with how to generate the video using FFMPEG. Here was the prompt:

“I’m using FFMPEG on Windows 11. I have a folder with images that have filenames in a sequence, from 1.jpg to 1295.jpg. I want to create a video out of them. Give it a framerate of 38”

ffmpeg -framerate 38 -start_number 1 -i "E:/Test/%d.jpg" -c:v libx264 -vf "fps=38,format=yuv420p" output.mp4

In this case I went with a framerate of 38 because for this particular video I wanted it to be under 30 seconds. The slower the framerate, the longer the output video will be. You can experiment with different ways to create the video, FFMPEG has been around for a long time and there is a lot of documentation and tutorials out there.

If you receive an error message when running FFMPEG (which happened to me a few times for various reasons), copy/paste it over to the AI you’re using and ask how to correct it. They’re great at figuring out what the problem is and difference solutions to fix code.

Results

Takeaways

This was pretty fun put together, especially using AI to figure out some basic PowerShell code I would not have been able to do on my own. I’ve tried for years to learn how to code using various languages but it just never sticks. Never imagined being able to ask a robot how to do something specific and it generating the script for me.

The code they created worked most of the time without any modifications, but there were a few errors, which led to having to Google, but that’s not really an issue. Learning is fun! If you’re going to do something similar to this, I hope you have fun as well putting AI to good use – maybe you can come up with a better process to do the same thing? Who knows.


SHARE THIS POST


keep comments civil - be cool hunny bunny

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x