Back

MP4 to GIF Converter

2024

Project

For the inspiration of my portfolio website, I wanted to create a moodboard in Miro. My biggest source of inspiration was Instagram, where I had liked many images and videos. Manually downloading this media seemed like an impossible task.

I started working on a web scraper. Unfortunately, I ran into problems. Downloading the last items in a series didn't work, and saving videos was difficult. Images weren't a problem, but videos were a challenge.

I looked for an alternative and discovered the possibility to request an overview of my Instagram likes as a JSON file. This file contained the URLs of all the media I had liked. With the bulk downloader WFdownloader, I could then download all images and videos at once.

A new problem arose. Miro didn't support MP4 videos, but it did support GIFs. Online converters were an option, but they were often limited to processing a small number of files at a time or one by one. This would take a lot of time given the amount of my downloaded media.

I decided to develop my own video-to-GIF converter. This gave me control over the process and allowed me to convert all my videos to GIF at once. It uses the ffmpeg library.


Code Explanation:

  1. Imports:
import os
import subprocess
import glob
import multiprocessing
  1. Folder Definition:
input_folder = r"map1"
output_folder = r"map2"

if not os.path.exists(output_folder):
    os.makedirs(output_folder)
  1. The convert_to_gif function:
def convert_to_gif(file_path):
    # Bepaal de naam van het output bestand
    base_name = os.path.basename(file_path)
    output_file = os.path.join(output_folder, f"{os.path.splitext(base_name)[0]}.gif")

    # Volledige pad naar ffmpeg.exe
    ffmpeg_path = r"locatie/download"  # Locatie waar je de ffmpeg hebt gedownload

    # Controleer of het GIF-bestand al bestaat
    if os.path.exists(output_file):
        print(f"{output_file} bestaat al. Sla over.")
        return  # Sla de conversie over als het bestand al bestaat

    # FFmpeg command
    command = [
        ffmpeg_path, '-i', file_path,
        '-vf', 'fps=10,scale=320:-1:flags=lanczos',
        '-c:v', 'gif',
        output_file
    ]

    # Voer het commando uit
    subprocess.run(command)
    print(f"Converted {file_path} to {output_file}")

The function convert_to_gif(file_path) is responsible for converting one MP4 video file to a GIF image. Below is a step-by-step explanation:

  1. Bestandsnaam bepalen:

  2. ffmpeg pad instellen:

  3. Controle op bestaand bestand:

  4. ffmpeg commando opbouwen:

  5. Commando uitvoeren en afronden:

  6. De main Functie:

def main():
    # Zoek naar MP4 bestanden in de input folder
    mp4_files = glob.glob(os.path.join(input_folder, '*.mp4'))

    # Gebruik multiprocessing om bestanden in bulk te converteren
    with multiprocessing.Pool() as pool:
        pool.map(convert_to_gif, mp4_files)

if __name__ == '__main__':
    main()

This function orchestrates the entire conversion process.

Complete code:

import os
import subprocess
import glob
import multiprocessing

input_folder = r"map1"
output_folder = r"map2"

if not os.path.exists(output_folder):
    os.makedirs(output_folder)

def convert_to_gif(file_path):
    # Bepaal de naam van het output bestand
    base_name = os.path.basename(file_path)
    output_file = os.path.join(output_folder, f"{os.path.splitext(base_name)[0]}.gif")
    
    # Volledige pad naar ffmpeg.exe
    ffmpeg_path = r"locatie/download"  # Locatie waar je de ffmpeg hebt gedownload
    
    # Controleer of het GIF-bestand al bestaat
    if os.path.exists(output_file):
        print(f"{output_file} bestaat al. Sla over.")
        return  # Sla de conversie over als het bestand al bestaat

    # FFmpeg command
    command = [
        ffmpeg_path, '-i', file_path, 
        '-vf', 'fps=10,scale=320:-1:flags=lanczos', 
        '-c:v', 'gif', 
        output_file
    ]
    
    # Voer het commando uit
    subprocess.run(command)
    print(f"Converted {file_path} to {output_file}")

def main():
    # Zoek naar MP4 bestanden in de input folder
    mp4_files = glob.glob(os.path.join(input_folder, '*.mp4'))
    
    # Gebruik multiprocessing om bestanden in bulk te converteren
    with multiprocessing.Pool() as pool:
        pool.map(convert_to_gif, mp4_files)

if __name__ == '__main__':
    main()

---