0

I am trying to record videos (visual + audio) while running a script through selenium python(headless-chrome) in google colab.
Using the Xvfb library and ffmpeg I was able to use a virtual display and record the video during the running of the script, but I could not get the audio.
I tried using pacmd and assigned and started a virtual sink but couldn't get any sound in the final outputs video.
For test purpose, I was trying to record youtube videos (I know there are different ways to download youtube videos directly but my real use case is not allowed to download videos from the webpage) so I need to screen record them.

# !sudo apt install ffmpeg
# !sudo apt install xvfb
# !pip install selenium
# !pip install xvfbwrapper
!pacmd load-module module-null-sink sink_name=MySink
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import os 
from datetime import date
import json
import time
from xvfbwrapper import Xvfb

import sys, getopt, time, subprocess, shlex

print('Sreencast website animation')
xvfb = Xvfb(width=720, height=720, colordepth=24)
xvfb.start()
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36')

wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)

url = "https://www.youtube.com/watch?v=bdlfdBiSzKw"

wd.get(url)
wd.save_screenshot("1.png") 
wait(wd, 10).until(EC.visibility_of_element_located((By.XPATH,'/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[1]/div/div/div/ytd-player/div/div/div[4]/button'))).click()

wd.save_screenshot("2.png") 
ffmpeg_stream = 'ffmpeg -y -r 30 -f x11grab -s 720x720 -i :%d+nomouse -c:v libx264rgb -crf 15 -preset:v ultrafast -c:a pcm_s16le -af aresample=async=1:first_pts=0 ouxkt.mkv'  % xvfb.new_display
args = shlex.split(ffmpeg_stream)
p = subprocess.Popen(args)
print(p)
time.sleep(10) # record for 10 secs
p.kill()
wd.quit()
xvfb.stop()

In the above code I am able to record the screen of a youtube video using a virtual display, and I also used the pacmd library to initialise a virtual sink but I did not get any audio in the video output, I have tried to add the virtual sink in the ffmpeg command still couldn't get any solution(I did not add that part in the code as it was giving some error)
Any help would be highly appreciated

kl kl
  • 66
  • 3
  • Please provide enough code so others can better understand or reproduce the problem. – Community Nov 24 '21 at 20:52
  • Hi @DebanjanB I have added the code trials that I have done, please let me know if you have any doubts/suggestions ,thanks – kl kl Nov 26 '21 at 18:58

0 Answers0