codingdirectional: Resize the video with python program

Hallo mein Freund, Jetzt ist es Zeit für ein weiteres tutorial, lasst uns beginnen! If you are following this website then you should know that recently I am exploring which is supposed to be a website which allows us to create game with python program and I am on the way to get into the game creation stage after solving some basic python questions on this site, but starting from today our plan has changed a little bit, I am going to start a brand new python project while continue to discover that above site at the same time, so you can expect more daily articles from this site soon on both the previous topic as well as the new topic, I might create a few more python related topics at the same time in the future as well. The project which we are going to create together is the video editing/streaming application project written in python with the help of the ffmpeg multimedia framework. I am going to create the project in windows os but if you are the mac or Linux user then you can just slightly modify the program code to suit your need, no problem at all.

FFmpeg is a complete, cross-platform solution tool uses to record, convert and stream audio and video. We are not going to use this tool directly on the command prompt instead we will use tkinter to create the video editing application user interface which wraps around this FFmpeg tool which can then be used to perform various video editing/streaming processes. Before we start you need to download and install FFmpeg on your computer first, you can search google for the keyword ‘FFmpeg’ and then download and install the tool on your computer through below search box.

We can now create a simple tkinter’s interface with a button which when we click on it will open up a file dialog box where we can then select a video to edit, in this example we will change the size of the video from it’s previous size of 1280 x 720 pixels to 1920 x 1080 pixels, the aspect ratio of the original video will be kept and the program will create a new video based on that new video size.

The UI is very simple

The entire program is as followed.

 from tkinter import * from tkinter import filedialog import os import subprocess  win = Tk() # Create instance win.title("Manipulate Video") # Add a title win.resizable(0, 0) # Disable resizing the GUI win.configure(background='black') # change background color  #  Create a label aLabel = Label(win, text="Click on below button to select a video", anchor="center", padx=13, pady=10, relief=RAISED,) aLabel.grid(column=0, row=0, sticky=W+E) aLabel.configure(foreground="white") aLabel.configure(background="black") aLabel.configure(wraplength=160)  # Open a video file def openVideo():         fullfilename = filedialog.askopenfilename(initialdir="/", title="Select a file", filetypes=[("Video file", "*.mp4; *.avi ")]) # select a video file from the hard drive         if(fullfilename != ''):                                  dir_path = os.path.dirname(os.path.realpath(fullfilename))                 os.chdir(dir_path)                 f = '1920.mp4' # the new output file format       ['ffmpeg', '-i', fullfilename, '-vf', 'scale=1920:-1', f]) # resize the video with ffmpeg                  action_vid = Button(win, text="Open Video", command=openVideo, padx=2) action_vid.grid(column=0, row=1, sticky=E+W) action_vid.configure(background='black') action_vid.configure(foreground='white')  win.mainloop() 

Because the FFmpeg tool runs on command prompt we will need to use the python’s subprocess module to pass in the FFmpeg’s command arguments as a list to the call method of the subprocess module.

Basically what the below line does is simply to resize the original video then saves it as a new video.['ffmpeg', '-i', fullfilename, '-vf', 'scale=1920:-1', f]) # resize the video with ffmpeg 

The time spends to resize the video will depend on the file size of the original video.

In this example the time spends to resize the video is a little bit longer than before

Friend, have you downloaded the application which we have created together previously? If not here is the link.


At the moment the application does not offer many selections of the video size for us to change, in the next article, we will include the selection box on this same application so we can then select any video’s size which we prefer. (If you have not yet subscribed to this site then click on the red bell button to subscribe).

Planet Python

Leave a Reply

Your email address will not be published. Required fields are marked *