0

I'm new to selenium web driver python and not really sure how to word this but here I go.

I have a script which allows you to record the screen using ffmpeg.

recoder_file_name = 'output.mkv'
proc = subprocess.Popen(['ffmpeg', '-f', 'gdigrab', '-framerate', '15', '-offset_x', '0', '-offset_y', '0', '-video_size', '1920x1080','-i', 'desktop', '-c:v', 'libx264', '-vprofile', 'baseline', '-g', '15', '-crf', '1', '-pix_fmt', 'yuv420p','-threads', '4', recoder_file_name])
# all of mine code
proc.kill()

problem with this is that I don't want to record all of the code only part of the code so I have done this

recoder_file_name = 'output.mkv'
proc = subprocess.Popen(['ffmpeg', '-f', 'gdigrab', '-framerate', '15', '-offset_x', '0', '-offset_y', '0', '-video_size', '1920x1080','-i', 'desktop', '-c:v', 'libx264', '-vprofile', 'baseline', '-g', '15', '-crf', '1', '-pix_fmt', 'yuv420p','-threads', '4', recoder_file_name])
#few part of my code
proc.kill()

recoder_file_name = 'output1.mkv'
proc = subprocess.Popen(['ffmpeg', '-f', 'gdigrab', '-framerate', '15', '-offset_x', '0', '-offset_y', '0', '-video_size', '1920x1080','-i', 'desktop', '-c:v', 'libx264', '-vprofile', 'baseline', '-g', '15', '-crf', '1', '-pix_fmt', 'yuv420p','-threads', '4', recoder_file_name])
#another part of my code
proc.kill()

This method is good but I have to keep writing that long code so I though I will make a function and just call the function

def recorder():
    proc = subprocess.Popen(['ffmpeg', '-f', 'gdigrab', '-framerate', '15', '-offset_x', '0', '-offset_y', '0', '-video_size', '1920x1080','-i', 'desktop', '-c:v', 'libx264', '-vprofile', 'baseline', '-g', '15', '-crf', '1', '-pix_fmt', 'yuv420p','-threads', '4', recoder_file_name])

recoder_file_name = 'output.mkv'
recorder()
driver.find_element(By.XPATH, "//*[@id='root']/div/div/div/form/input[1]").send_keys(username)
driver.find_element(By.XPATH, "//*[@id='root']/div/div/div/form/input[2]").send_keys(password)
driver.find_element(By.XPATH, "//*[@id='root']/div/div/div/form/button").click()

proc.kill()

Now I am stuck here were it will record but it wont stop recording because proc.kill() is unresolved reference 'proc'. I don't know how to make proc.kill() communicate with def recorder() function. Is there a solution for this or even a better way?

Also sorry for the title I cant think any better than this.

thanks @Rotem His solution was this

def recorder():
    proc = subprocess.Popen(['ffmpeg', '-f', 'gdigrab', '-framerate', '15', '-offset_x', '0', '-offset_y', '0', '-video_size', '1920x1080','-i', 'desktop', '-c:v', 'libx264', '-vprofile', 'baseline', '-g', '15', '-crf', '1', '-pix_fmt', 'yuv420p','-threads', '4', recoder_file_name])
    return proc
proc = recorder()
recoder_file_name = 'output.mkv'
recorder()
#part of my code
proc.kill()

recoder_file_name = 'output1.mkv'
recorder()
#another part of my code
proc.kill()

For me this will work but if you have a better solution feel free to post for others <3

WESTKINz
  • 257
  • 1
  • 6
  • 18
  • 1
    You may return `proc`: `def recorder():` ... `proc = `... return `proc`. And use it `proc = recorder()` ... `proc.kill()`. Note: using `proc.kill()` is not a good practice for stop recording. You may look for a way for closing FFmpeg gracefully. Take a look at the following [post](https://stackoverflow.com/questions/67276793/output-always-corrupt-from-ffmpeg-using-selenium-python). Check both answers (I think the solution depends on the version of FFmpeg). – Rotem Nov 26 '21 at 11:21
  • thank you, I will try your method – WESTKINz Nov 26 '21 at 11:32

0 Answers0