无YouTube API v3权限,用Colab/Apps Script+Selenium转Drive视频到YouTube的问询
问题解答:Google Drive视频转YouTube(无API权限/仅用Colab&Apps Script)
1. Google Apps Script中能否安装Selenium WebDriver?
- 不行。Google Apps Script运行在云端沙箱环境,仅支持原生JS/TS及官方内置服务(如Drive Service),无法安装或运行Selenium WebDriver——Selenium依赖浏览器驱动和本地/虚拟机的浏览器环境,而Apps Script没有提供此类底层运行权限。
2. 能否将Google Drive挂载为Google Apps Script的虚拟存储?
- 无需额外挂载,Google Apps Script内置
DriveApp服务,可直接读写Google Drive文件:- 支持列出文件、获取下载链接、修改属性、复制/移动文件等操作;
- 视频文件可通过
DriveApp.getFileById(文件ID).getDownloadUrl()获取临时下载链接(注意链接有有效期限制)。
3. 不启用YouTube API v3,能否在Google Colab中用Selenium完成转移?
- 完全可以。Google Colab提供完整Python环境,支持安装Selenium和Chrome驱动,可模拟手动上传流程,无需依赖YouTube API。
可行解决方案(Google Colab + Selenium)
步骤1:挂载Google Drive到Colab
在Colab单元格中运行以下代码,按提示完成授权登录:
from google.colab import drive drive.mount('/content/drive')
挂载后Drive文件会出现在/content/drive/MyDrive/路径下。
步骤2:安装Selenium及Chrome驱动
执行以下命令完成环境配置:
!pip install selenium !apt-get update !apt install chromium-chromedriver !cp /usr/lib/chromium-browser/chromedriver /usr/bin
步骤3:编写Selenium上传脚本
核心逻辑是模拟手动登录YouTube Studio并上传视频,示例框架如下:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # 配置Colab适配的Chrome参数 chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') # 初始化浏览器 driver = webdriver.Chrome('chromedriver', options=chrome_options) # 打开YouTube Studio上传页(替换为你的频道ID) driver.get('https://studio.youtube.com/channel/你的频道ID/uploads') # 手动登录环节:去掉--headless参数可显示浏览器窗口完成登录,登录后按回车继续 input("登录完成后按回车继续...") # 点击上传按钮 upload_btn = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="upload-icon"]'))) upload_btn.click() # 传入Drive中视频路径 file_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//input[@type="file"]'))) file_input.send_keys('/content/drive/MyDrive/你的视频文件.mp4') # 填写视频信息 title_input = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="textbox"]'))) title_input.send_keys('视频标题') description_input = driver.find_element(By.XPATH, '//*[@id="textbox"]/following-sibling::div') description_input.send_keys('视频描述') # 设置隐私权限(示例为公开) public_btn = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@name="PUBLIC"]'))) public_btn.click() # 发布视频 publish_btn = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="done-button"]'))) publish_btn.click() # 根据视频大小调整等待时间 time.sleep(120) # 关闭浏览器 driver.quit()
注意:
- 登录环节若使用无头模式,可提前导出YouTube登录Cookie并导入脚本实现自动登录;
- 上传等待时间需根据视频大小和网络速度灵活调整。
内容的提问来源于stack exchange,提问作者manistor hiliston
相关产品推荐
相关产品推荐

