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()
@Iko2023
Copy link

Iko2023 commented Sep 9, 2018

Hello
thank you for this.
I have read your comments on several forums concerning your attempts to record video from a screencast. I am glad to see you manage to find a solution.
How are the results, did you manage to get smooth recording ? Is it possible to see an example clip ?
I am looking forward to test this, but I can't manage to use xvfb on os x.
Thank you
Iko2023

@t-io
Copy link
Author

t-io commented Sep 11, 2018

hey @Iko2023,
The results are okayish. I wanted to create animations with js/css and dynamically record a video from it.
We never developed the product but it would be possible this way.
Another way to go would be to use blender with predefine animations, automate it and wrap it in a service.
But for this you need some understanding of 3d software. For a sophisticated product I would go this way.
Good luck.

Here are some examples:
chrome render
firefox render
small file size

@Iko2023
Copy link

Iko2023 commented Sep 11, 2018

Hey @t-io thanks so much for your reply ! My goal is similar, I create brief QT movies from css/jquery animations. I can easily produce collections of videos with the same "themes", CSS allows that easily. At the moment I record the video with quicktime capture, then crop the captured video. The quality of the videos is good but the workflow is cumbersome. I am looking for a solution to automate the video creation. I am not trying to build a product, but a production environment for inhouse use. The videos you recorder are not bad, especially the small file size. Thank you for the Blender suggestion, I will look into that, but, correct me if I am wrong, scripting in blender is based on python, not js right ? Thanks again !

@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