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