Python先顺序分组然后打乱分组顺序

# 合并ts视频
import os,time ,random, shutil

# 定义循环次数
loop_times = 10

#用来分组排序的数组
mySortArr = []
path = r"D:\XXX\视频ts文件夹"
for file in os.listdir(path):
if file.endswith('.ts'):
mySortArr.append(file)

#每个分组最小包含文件数
maxFileNum = 4
def find_all_index(arr, item):
return [i for i, a in enumerate(arr) if a == item]
randomListMax = len(mySortArr)*100
def sortA(filelist):
tempGroupList = []
#分组
groupList = []
tempGroupLIst = []
tempGroupAppendIndex=0
while tempGroupAppendIndex < randomListMax+1:
groupList.append(0)
tempGroupAppendIndex += 1
i = 0
countNum = 0
for files in filelist: #遍历所有文件
i = i + 1
countNum +=1
tempGroupList.append(files)
randomIndex = random.randint(0,randomListMax)
hasPlaceholder = find_all_index(groupList,tempGroupLIst)
while (len(hasPlaceholder)!=0):
randomIndex = random.randint(0,randomListMax)

if countNum >= maxFileNum :
countNum = 0
# print(randomIndex,"randomIndex")
rIndex = 0
for tempItem in tempGroupList:
rIndex+=1
groupList[randomIndex+rIndex] = tempItem
tempItemR = reversed(tempGroupList) # 倒序
for tempItem in tempItemR:
groupList[randomIndex+rIndex+len(tempGroupLIst)] = tempItem
rIndex+=1
tempItemR = []
tempGroupList = []

removeGroupList = groupList
listRes = []
for groupItemOld in removeGroupList:
if(groupItemOld!=0):
listRes.append(groupItemOld)
# random.shuffle(filelist)
# print(listRes,"listRes")
return listRes
#/sort$############################

mySortArr = sortA(mySortArr)

# 执行循环遍历
for i in range(1, loop_times + 1):
with open(f"{i}.txt", 'w', encoding='utf-8') as f:
for content in mySortArr:
f.write(f"file '{content}'\n")
# print(f"{i}.txt 文件生成成功!")

#quit()
# 合并txt文件
os.system('copy /B ?*.txt output.txt')
print("合并txt文件成功!")

# 删除生成的i.txt文件
for i in range(1, loop_times + 1):
os.remove(f"{i}.txt")
print("i.txt 文件删除成功!")
# 执行ffmpeg合并
os.system('ffmpeg -f concat -safe 0 -i output.txt -c copy 合成视频.mp4')
print("ffmpeg合并成功!")

print("ok")

# 删除 output.txt 文件
os.remove('output.txt')
print("output.txt 文件删除成功!")


# 删除当前目录下所有.ts格式的视频文件
for filename in os.listdir('.'):
if filename.endswith('.ts'):
os.remove(filename)
print(".ts格式 文件删除成功!")

# 删除所有前缀为images_的文件夹
for foldername in os.listdir('.'):
if foldername.startswith('images_'):
shutil.rmtree(foldername)

print("images_ 文件删除成功!")