import requests
import os
import subprocess
#https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/3fd2d78e1ebba085-529617cf38bbad5860227fbdf3a41546-hls_720p_2.00003.ts?pkey=ABC_F8k9Ed6OSnAdir8rrRmbYfeU39b5CvYeJQ3ttw8ZLQzlfk1NZNLJOlmwW-9ENIIuNLhhWVf1944ksBv4VUNYsnJ_PuQXkS1wnc6CMJU3WX_8TT_2pkCblE-drmscnJu7BqNVNHyao0hR22lfcMqkEN-NmMfEgdnDBnQwlVjheLff_cRVzrjGiUFBu2fsJ7C8-i4cY9hZUfUYL6Mqtn68EZg-GPKaFuN637C0xpdRS3QReVF6C8DVXZpfl5z8mQQ&safety_id=AAIDfT-EEfFKy5-dwuxfmICd
base_url = “https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/3fd2d78e1ebba085-529617cf38bbad5860227fbdf3a41546-hls_720p_2.”
pkey =“ABC_F8k9Ed6OSnAdir8rrRmbYfeU39b5CvYeJQ3ttw8ZLQzlfk1NZNLJOlmwW-9ENIIuNLhhWVf1944ksBv4VUNYsnJ_PuQXkS1wnc6CMJU3WX_8TT_2pkCblE-drmscnJu7BqNVNHyao0hR22lfcMqkEN-NmMfEgdnDBnQwlVjheLff_cRVzrjGiUFBu2fsJ7C8-i4cY9hZUfUYL6Mqtn68EZg-GPKaFuN637C0xpdRS3QReVF6C8DVXZpfl5z8mQQ&safety_id=AAIDfT-EEfFKy5-dwuxfmICd”
创建一个文件夹用于存放视频片段,取名为"video_segments"
if not os.path.exists(“sp”):
os.mkdir(“sp”)
初始化片段编号
segment_number = “00000”
用于记录找到的最大片段编号
max_segment_number = None
while True:
url = f"{base_url}{segment_number}.ts?pkey={pkey}"
response = requests.get(url)
if response.status_code == 200:
# 如果片段存在,保存到文件夹并更新最大片段编号
file_path = os.path.join(“sp”, f"video_{segment_number}.ts")
with open(file_path, “wb”) as f:
f.write(response.content)
max_segment_number = segment_number
print(f"视频片段 {max_segment_number} 下载成功")
else:
# 如果片段不存在,跳出循环
break
# 递增片段编号,确保格式为5位数字
segment_number = f"{int(segment_number):05d}"
segment_number = f"{int(segment_number)+1:05d}"
if max_segment_number is not None:
# 创建用于FFmpeg合并的输入文件列表(采用文本文件方式)
video_segments_folder = “C:\Users\Administrator\PycharmProjects\pythonPC\sp”
input_file_path = os.path.join(video_segments_folder, “input.txt”)
with open(input_file_path, “w”) as f:
for root, dirs, files in os.walk(video_segments_folder):
for file in files:
if file.endswith(“.ts”):
file_path = os.path.join(root, file).replace(“\”, “/”) # 确保路径格式为正斜杠格式,符合多数FFmpeg使用场景
f.write(f"file ‘{file_path}’\n")
# 定义FFmpeg命令,使用 -f concat结合文本文件方式来合并视频片段
ffmpeg_command = f'ffmpeg -f concat -safe 0 -i {input_file_path} -c copy 熊出没番外篇环保篇.mp4'
# 使用subprocess模块调用命令行执行FFmpeg命令
subprocess.call(ffmpeg_command, shell=True)
print("视频合并完成!")
else:
print(“未找到任何视频片段。”)