if response.status_code == 200: withopen(output_filename, 'wb') as f: for chunk in response.iter_content(chunk_size=1024): if chunk: f.write(chunk) print(f"Downloaded video {i + 1}") else: print(f"Failed to download video from {url}")
# 使用线程池进行多线程下载 with ThreadPoolExecutor(max_workers=10) as executor: for i inrange(start_index, end_index + 1): executor.submit(download_video_segment, i, base_url, output_directory)
# 检查下载并合并视频片段 video_files = [os.path.join(output_directory, f"video_{i}.ts") for i inrange(start_index, end_index + 1)] missing_files = [f for f in video_files ifnot os.path.exists(f)]
if missing_files: print("以下视频片段缺失,无法进行合并:", missing_files) else: # 所有文件都存在,可以合并视频 video_files_str = "|".join(video_files) output_file_name = f"{video_name}.mp4" command = f"ffmpeg -i \"concat:{video_files_str}\" -c copy {output_file_name}" os.system(command) print(f"Videos have been merged into {output_file_name}!")