Friday, January 11, 2019

codingdirectional: Reduce video frame rate with Python and FFmpeg

Hi, it has been two days already I am not posting any article because I am busy working on the new gaming project as well as taking care of the offline business. Today we will continue with the video editing application project which we have started a while ago by changing the frame rate of a video to make it slower.

The original frame rate of the video which we are about to change is around 29.97 frames/s and we are going to change it’s frame rate to 24 frames/s using the FFmpeg tool and python program.

Original Video Frame Rate

Below is the entire program which we will use to change the frame rate of the video.

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
        if(fullfilename != ''):
                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 and format
                #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 loop 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)
action_vid.configure(background='black')
action_vid.configure(foreground='white')

win.mainloop()

The new output is as follow.

The new frame rate of that same video

By changing the frame rate of that video we will make those objects inside the video appear slower, we also have reduced the file size of that video from 45.7 MB to 8.54 MB but the video’s duration remains unchanged. If you wish to reduce the file size of a video but do not wish to trim the duration of the video, you can actually select a frame rate below the original video but not that far apart from the original video so the movement of the object in the video will still appear to move in a normal speed but the original video size has actually been reduced significantly.

We will continue to develop this application in the next chapter so stay tuned.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...