能否使用Google Python脚本上传YouTube视频描述文件?
当然可以实现!
你完全可以修改现有的Python脚本,让它从外部文件读取视频描述内容并上传到YouTube。下面是具体的修改步骤和代码示例:
步骤1:添加描述文件的命令行参数
首先,我们需要在脚本的参数解析部分,新增一个用于指定描述文件路径的参数。找到脚本里的argparser相关代码,修改成这样:
def main(): argparser.add_argument("--file", required=True, help="Video file to upload") argparser.add_argument("--title", help="Video title", default="Test Title") argparser.add_argument("--description", help="Video description", default="") # 新增描述文件参数 argparser.add_argument("--description-file", help="Path to file containing video description") argparser.add_argument("--category", default="22", help="Numeric video category.") argparser.add_argument("--keywords", help="Video keywords, comma separated", default="") argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES, default="private", help="Video privacy status.") args = argparser.parse_args()
步骤2:修改initialize_upload函数,读取描述文件内容
接下来,在initialize_upload函数里,我们需要判断用户是否提供了描述文件,如果有就读取文件内容作为视频描述;如果没有,就沿用原来的options.description参数值:
def initialize_upload(youtube, options): tags = None if options.keywords: tags = options.keywords.split(",") # 处理描述内容:优先读取文件 video_description = options.description if options.description_file: try: with open(options.description_file, 'r', encoding='utf-8') as f: video_description = f.read() except FileNotFoundError: print(f"Error: Description file {options.description_file} not found. Using default description instead.") except Exception as e: print(f"Error reading description file: {str(e)}. Using default description instead.") body=dict( snippet=dict( title=options.title, description=video_description, # 使用读取到的描述内容 tags=tags, categoryId=options.category ), status=dict( privacyStatus=options.privacyStatus ) ) # 后面的上传代码保持不变...
步骤3:运行脚本时指定描述文件
现在你可以这样运行脚本,用外部文件作为视频描述:
python your_script.py --file "your_video.mp4" --title "My Video Title" --description-file "video_description.txt" --privacyStatus public
注意事项
- 确保描述文件使用UTF-8编码,避免出现乱码问题
- 如果同时指定了
--description和--description-file,脚本会优先使用描述文件里的内容 - 可以在描述文件里添加换行、特殊字符,YouTube会正确解析这些格式
这样修改后,你的脚本就支持从外部文件读取视频描述并上传啦!
内容的提问来源于stack exchange,提问作者Iván Rodríguez
相关产品推荐
相关产品推荐

