Turn video into black and white with python

Hi, in this quick article we are going to turn a colorful video into a black and white video using python with the help of the FFmpeg tool. We will use the equalizer filter from FFmpeg tool to get the job done. If you have missed the previous steps to resize and to add music into the video then you can read the previous article about it. Below are two pictures showing the video scene before and after applying the equalizer filter.

Before editing
After editing

Playing around with the FFmpeg’s eq filter by yourself to find out more effects. Below is the program which changes the color 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
        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
                subprocess.call(['ffmpeg', '-i', f, '-vf', 'eq=contrast=1.3:brightness=-0.03:saturation=0.01', '-y', f2]) # adjust the saturation contrast and brightness of video

                
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()

We are supposed to edit the user interface of this video editing application today but instead, we will leave that to the next round since there is already enough thing for you to learn in this chapter.

Leave a Reply

avatar
  Subscribe  
Notify of