Hello, just like what I have promised that I will write another article today so here is another short article regarding the ongoing video editing project written in python with the help of FFmpeg. In below program we will first remove the original audio from the video and then inserted music into that same video, the music is longer than the video itself so we will trim the music file to match the exact length of the video. The program below will also resize the video to the size which has been selected by the user.
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)
aLabel.configure(foreground="black")
aLabel.configure(background="white")
aLabel.configure(wraplength=110)
# 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 in the comno box
new_size = str(scale_vid)
dir_path = os.path.dirname(os.path.realpath(fullfilename))
os.chdir(dir_path)
f = new_size + '.mp4' # the new output file name/format
f2 = f + '1.mp4' # video without audio
#subprocess.call(['ffmpeg', '-stream_loop', '2', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', f]) # resize and loop the video with ffmpeg
#subprocess.call(['ffmpeg', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', '-r', '24', f]) # resize and speed up the video with ffmpeg
#subprocess.call(['ffmpeg', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', f]) # resize the video with ffmpeg
#subprocess.call(['ffmpeg', '-i', f, '-ss', '00:02:30', '-y', f2]) # create animated gif starting from 2 minutes and 30 seconds to the end
subprocess.call(['ffmpeg', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', f]) # resize the video with ffmpeg
subprocess.call(['ffmpeg', '-i', f, '-c', 'copy', '-y', '-an', f2]) # remove audio from the original video
subprocess.call(['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
action_vid = Button(win, text="Open Video", command=openVideo, padx=2)
action_vid.grid(column=0, row=2, sticky=E+W)
action_vid.configure(background='black')
action_vid.configure(foreground='white')
win.mainloop()
This video application is picking up and more features will be included in the future, we will tidy up the user interface first before going even further in the next article.
from Planet Python
via read more
No comments:
Post a Comment