Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ScreenRecording xvfb
from selenium import webdriver
import sys, getopt, time, subprocess, shlex
from xvfbwrapper import Xvfb
def run():
print('Sreencast website animation')
xvfb = Xvfb(width=1280, height=720, colordepth=24)
xvfb.start()
#browser = webdriver.Chrome()
browser = webdriver.Firefox()
url = 'https://s3-eu-west-1.amazonaws.com/movefast-quickhost/web-animation-test/index.html'
destination = 'movie.flv'
browser.get(url)
# normal quality, lagging in the first part on the video. filesize ~7MB
# ffmpeg_stream = 'ffmpeg -f x11grab -s 1280x720 -r 24 -i :%d+nomouse -c:v libx264 -preset superfast -pix_fmt yuv420p -s 1280x720 -threads 0 -f flv "%s"' % (xvfb.new_display, destination)
# high quality, no lagging but huge file size ~50MB
ffmpeg_stream = 'ffmpeg -y -r 30 -f x11grab -s 1280x720 -i :%d+nomouse -c:v libx264rgb -crf 15 -preset:v ultrafast -c:a pcm_s16le -af aresample=async=1:first_pts=0 out.mkv' % xvfb.new_display
args = shlex.split(ffmpeg_stream)
p = subprocess.Popen(args)
print(p)
time.sleep(30) # record for 30 secs
browser.quit()
xvfb.stop()
if __name__ == "__main__":
run()
@jzvi12
Copy link

jzvi12 commented Jun 17, 2020

Hey, does your script support recording audio as well?

Does it work in a headless Linux environment?

Is it possible to add more code to automate some inputs in the browser before recording?

@t-io
Copy link
Author

t-io commented Jun 18, 2020

Hey @jzvi12,
this script was never intended to record audio, only video. It works indeed in a headless browser webdriver.Firefox() and the frames are recorded by xvfb.
Of course, it's possible to automate some inputs beforehand, check out selenium for browser automation.

@victor-zhong
Copy link

victor-zhong commented Mar 4, 2022

@t-io

I have tried you code with

from selenium import webdriver
import sys, getopt, time, subprocess, shlex
from xvfbwrapper import Xvfb
from selenium.webdriver.chrome.options import Options


def run():
    print('Sreencast website animation')
    xvfb = Xvfb(width=1280, height=720, colordepth=24)
    xvfb.start()
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox");
    chrome_options.add_argument("--disable-dev-shm-usage");
    browser = webdriver.Chrome(options=chrome_options)
    # browser = webdriver.Firefox()

    url = 'https://s3-eu-west-1.amazonaws.com/movefast-quickhost/web-animation-test/index.html'
    destination = 'movie.flv'
    browser.get(url)

    # normal quality, lagging in the first part on the video. filesize ~7MB
    ffmpeg_stream = 'ffmpeg -f x11grab -s 1280x720 -r 24 -i :%d+nomouse -c:v libx264 -preset superfast -pix_fmt yuv420p -s 1280x720 -threads 0 -f flv "%s"' % (xvfb.new_display, destination)

    # high quality, no lagging but huge file size ~50MB
    ffmpeg_stream = 'ffmpeg -y -r 30 -f x11grab -s 1280x720 -i :%d+nomouse -c:v libx264rgb -crf 15 -preset:v ultrafast -c:a pcm_s16le -af aresample=async=1:first_pts=0 out.mkv'  % xvfb.new_display
    args = shlex.split(ffmpeg_stream)
    p = subprocess.Popen(args)
    print(p)

    time.sleep(10) # record for 30 secs
    print("DONE")
    p.kill()
    browser.quit()
    xvfb.stop()



if __name__ == "__main__":
    run()

but it won't record unless I set chrome not under headless mode.

" It works indeed in a headless browser webdriver.Firefox()" how can we know this firefox is working under headless mode? or the I misunderstand the headless meaning here.

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment