import os
import subprocess
def play_videos_in_folder(folder_path):
# 获取所有视频文件
files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith(('.mp4', '.mkv'))]
idx = 0 # 视频文件索引
while True:
try:
# 播放视频
cmd = ['ffmpeg', '-i', files[idx], '-c', 'copy', '-f', 'mp4', 'pipe:1']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
# 读取stdout中的视频流并播放
while True:
raw = proc.stdout.read(1024)
if not raw:
break
# 这里可以选择将raw写入视频播放器来实现播放
print(raw)
# 视频播放结束则轮播下一个文件
idx = (idx + 1) % len(files)
except KeyboardInterrupt:
# 如果中断播放,则记录当前播放的位置
pos = proc.stdout.tell()
with open('resume_position.txt', 'w') as f:
f.write(f'{idx},{pos}')
proc.kill()
break
except:
idx = (idx + 1) % len(files)
continue
if __name__ == '__main__':
folder_path = 'video_folder_path'
play_videos_in_folder(folder_path)
标签:__,视频,idx,中断,path,folder,播放
From: https://www.cnblogs.com/qcy-blog/p/17836281.html