How to record just a window with ffmpeg

Speaking about ffmpeg, the free software for editing video with a command line interface, I want to tell you how to record just the content of a window on the screen. I will show you how I did it in my last post having this as result:

This was the result of the little script below. As you can see, we gave the window (root) a title (“Trace”), so we added that title in the command of ffmpeg to target it for the recording. You have to check for your audio device too. I will put the commands to do it at the end of the code.

from tkinter import *

def copycat(sv, lb):
    lb['text'] = sv.get()

def labentry():
	sv = StringVar() # Create the StringVar
	sv.trace("w", lambda name, index, mode, sv=sv: copycat(sv, lb))
	# copycat is usually called callback
	e = Entry(root, textvariable=sv)
	lb = Label(root, text = "")
	lb.pack()
	e.pack()
	e.focus()

root = Tk()
root.title("Trace")
labentry() # => create label and entry
root.mainloop()

You can see the root.title at the end of the code.

How to record a window

ffmpeg -rtbufsize 1500M -f dshow -i audio="Microfono (8- Logitech USB Headset)" -f gdigrab -framerate 30 -draw_mouse 1 -i title=Trace -pix_fmt yuv420p -profile:v baseline -y Huangbaohua.mp4

pause

You can see that the title of the window we targeted is “Trace” (we gave this name to the window in the code above).

Save this text into a batch file (.bat at the end of the name) and run it after you saved it.

How to check your audio device and other devices names

ffmpeg -list_devices true -f dshow -i dummy

pause

Save also this as .bat file and run it.

Record just a window for some time

To record a specific window (by its name) and for a specific amount of time, you can use this code and save it as a .bat file

ffmpeg -rtbufsize 1500M -f dshow -i audio="Microfono (8- Logitech USB Headset)" -f gdigrab -framerate 30 -draw_mouse 1 -i title=Trace -pix_fmt yuv420p -profile:v baseline -y -t 00:00:30 win30sec.mp4

pause

The duration is the -t 00:00:30

You can change it to the duration you want.

Ffmpeg & Python for videos

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.