import subprocess
import os



# 获取所有文件
def getAllFiles(fire_dir):
    filepath_list = []
    for root,folder_names,file_names in os.walk(fire_dir):
        for file_name in file_names:
            file_path = root+os.sep+file_name
            filepath_list.append(file_path)
            print(file_path)
    print(filepath_list)
    return filepath_list

# 获取视频的时长
def get_length(filename):
    result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
                             "format=duration", "-of",
                             "default=noprint_wrappers=1:nokey=1", filename],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    return float(result.stdout)

#按照秒数切割,多余直接删除
def qiegeByMiaoShu(fire_dir,delta_X,save_path):
    #fire_dir 文件加、夹目录
    #delta_X  切割秒数
    #save_path  输出文件目录
    try:
        video_list = os.listdir(fire_dir)
        mark = 0
        for file_name in video_list:



            print("file_name:")
            print(file_name)
            f_houzui = os.path.splitext(file_name)[-1]

            print("f_houzui:")
            print(f_houzui)
            qudiaohouzui_file_name = file_name.strip(f_houzui)

            shichang = get_length(os.path.join(fire_dir, file_name))
            print("shichang:")
            print("%s 秒" % shichang)
            min = int(shichang) // 60  # file_name视频的分钟数
            print("min")
            print(min)
            second = int(shichang) % 60  # file_name视频的秒数
            print("second:" )
            print(second)
            duanshu = int(shichang)//2
            print("duanshu:")
            print(duanshu)
            for i in range(min + 1):
                if second >= delta_X:  # 至少保证一次切割
                    start_time = 0
                    end_time = start_time + delta_X
                    for j in range((second // int(delta_X)) + 1):
                        min_temp = str(i)
                        start = str(start_time)
                        end = str(end_time)
                        # crop video
                        # 保证两位数
                        if len(str(min_temp)) == 1:
                            min_temp = '0' + str(min_temp)
                        if len(str(start_time)) == 1:
                            start = '0' + str(start_time)
                        if len(str(end_time)) == 1:
                            end = '0' + str(end_time)

                        # 设置保存视频的名字
                        if len(str(mark)) < 6:
                            print("少于6")
                            name = str(qudiaohouzui_file_name)+"_"+str(j)+'_'+'0' * (6 - len(str(mark)) - 1) + str(mark)
                        else:
                            print("大于6")
                            name = str(qudiaohouzui_file_name)+"_"+str(j)+'_'+str(mark)
                        print("name:")
                        print(name)
                        command = 'ffmpeg -n -i {} -ss 00:{}:{} -to 00:{}:{} -strict -2 {} '.format(os.path.join(fire_dir,file_name),
                                                                                                min_temp, start, min_temp, end,
                                                                                                os.path.join(save_path,
                                                                                                             'id' + str(
                                                                                                                 name)) + '.mp4')
                        mark += 1
                        os.system(command)
                        if i != min or (i == min and (end_time + delta_X) < second):
                            start_time += delta_X
                            end_time += delta_X
                        elif (end_time + delta_X) <= second:
                            start_time += delta_X
                            end_time += delta_X
                        elif (end_time + delta_X) > second:  # 最后不足delta_X的部分会被舍弃
                            break
    except Exception as e:
        print("报错:%s" % str(e))

if __name__ == "__main__":
    fire_dir = r'F:\存储盘\古风美女素材下载\video\heng\dyonem\da1280\1280x720\daijianji'  # 待切割视频存储目录
    delta_X = 2  # 每2s切割
    save_path = save_path = r'F:\存储盘\古风美女素材下载\video\heng\dyonem\da1280\1280x720\shuchu'
    qiegeByMiaoShu(fire_dir,delta_X,save_path)

 

posted on 2023-06-27 10:29  大话人生  阅读(96)  评论(0编辑  收藏  举报