Python and ffmpeg: grabs some seconds from an mp4 video

Here’s the code to grab some seconds from an mp4 video and save them as a new file with Python. This could be a start to work on a video and grab only the best part of it. You need to install the free software ffmpeg. Check in my posts to see how. Python just asks you to input the file and the seconds of the start and the end.

import os, glob

from tkinter import filedialog
import tkinter as tk

root = tk.Tk()
root.filename = filedialog.askopenfilename(initialdir=".",
    title="Select file", filetypes=(("jpeg files", "*.mp4"), ("all files", "*.*")))
if root.filename == "":
    root.destroy()
print(root.filename)
name = root.filename
print("Tell me where to start and end...")
start = input("Start (seconds):")
end = input("End (seconds):")
# os.system(f"ffmpeg -i \"{name}\" -ss {start} -t {end} start_of_output.mp4")
# alternative command
os.system(f"ffmpeg -i {name} -ss {start} -t {end} -c copy start_of_output.mp4")

The video that shows how it works

Ffmpeg & Python for videos

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.