Convert video from one format to another with python

Hello, and well come back. In this article, we will input the last feature for our video editing application before we tidy up the entire application’s user interface and then upload this free application to the online software store for others to enjoy. As usual, we will use the FFmpeg tool together with a python program to convert a video from the mp4 format to the webm format. This program will first resize the video to the size which the user has selected and then start to convert the video to the new format then saves that new video to a new file. Below is the entire program.

from tkinter import *
from tkinter import filedialog
import os
import subprocess
import tkinter.ttk as tk

win = Tk() # Create instance
win.title("Manipulate Video") # Add a title
win.resizable(0, 0) # Disable resizing the GUI
win.configure(background='white') # change background color

#  Create a label
aLabel = Label(win, text="Select video size and video", anchor="center", padx=13, pady=10, relief=RAISED)
aLabel.grid(column=0, row=0, sticky=W+E)

# Create a combo box
vid_size = StringVar() # create a string variable
preferSize = tk.Combobox(win, textvariable=vid_size) 
preferSize['values'] = (1920, 1280, 854, 640) # video width in pixels
preferSize.grid(column=0, row=1) # the position of combo box
preferSize.current(0) # select item one 

# 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
        #audiofilename = filedialog.askopenfilename(initialdir="/", title="Select a file", filetypes=[("Audio file", "*.mp4; *.ogg ")]) # select a new audio file from the hard drive
        if(fullfilename != ''): #and audiofilename != ''):
                scale_vid = preferSize.get() # retrieve value from the comno box
                new_size = str(scale_vid)
                dir_path = os.path.dirname(os.path.realpath(fullfilename))
                f = new_size  + '.mp4' # the new output file name/format
                f2 = f + '.webm' # webm video
      ['ffmpeg', '-stream_loop', '2', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', f]) # resize and loop the video with ffmpeg
      ['ffmpeg', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', '-r', '24', f]) # resize and speed up the video with ffmpeg
      ['ffmpeg', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', f]) # resize the video with ffmpeg
      ['ffmpeg', '-i', f, '-ss', '00:02:30', '-y', f2]) # create animated gif starting from 2 minutes and 30 seconds to the end
      ['ffmpeg', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', f]) # resize the video with ffmpeg
      ['ffmpeg', '-i', f, '-c', 'copy', '-y', '-an', f2]) # remove audio from the original video
      ['ffmpeg', '-i', f2, '-i', audiofilename, '-shortest', '-c:v', 'copy', '-c:a', 'aac', '-b:a', '256k', '-y', f]) # add audio to the original video, trim either the audio or video depends on which one is longer
      ['ffmpeg', '-i', f, '-vf', 'eq=contrast=1.3:brightness=-0.03:saturation=0.01', '-y', f2]) # adjust the saturation contrast and brightness of video
      ['ffmpeg', '-i', f, '-y', f2]) # converting the video with ffmpeg

action_vid = Button(win, text="Open Video", command=openVideo, padx=2)
action_vid.grid(column=0, row=2, sticky=E+W)


Finally, the last piece of code has been completed. We will continue to modify all those features in this application as well as to tidy up the user interface of this video editing application starting from the next chapter onward so stay tuned.

I always want to write more articles per day but due to lack of stamina I always end up with only one article per day. I am working on the energy and stamina part now and hopefully, I will be able to write a few more articles per day. Writing article needs concentration thus stamina training is a must if we need to write more. Tomorrow I am off to take care of the offline business, see you again the day after tomorrow.

Leave a Reply

Notify of