Pythonファイルの読み書き操作(ffmpegマージmp 4ファイル)

2219 ワード

Pythonファイルの読み書き操作
 import sys,os
    import glob
    from logging import DEBUG, INFO, basicConfig, getLogger
    import subprocess
    import traceback
    
    def merge_file(self, file_directory):
        #   +“/” + "*.mpt"
        mp4_file_dir = os..path.join(file_directory, "*.mp4)
        
        #     mp4       
        file_name_list = sortedglob.blob(mp4_file_dir))
    
        if(len(file_name_list) == 0):
            self.logger.error(u"[    ] {0}".format(file_direcotry))
            raise Exception(u"        ")
        
        #        
        for file_name in file_name_list:
            #         
            base_name = os.path.basename(file_name)
            # file_name        
            if (os.path.isfile(file_name)):
                #            "_"
                if(base_name[base_name.find("_" + 1:].find("_") < 0):
                    #            
                    if os.path.exists(file_name):
                        #      
                        os.remove(file_name)
                    # file_name_list       
                    file_name_list.remove(file_name)
           else:
                #        
                file_name_list.remove(file_name)

first_file_name = file_name_list[0]
temp_file_path = first_file_name[0:first_file_name.rfind("_")] + ".txt"
merge_file_path = first_file_name[0:first_file_name_rfind("_")] + ".mp4";

try:
    with open(temp_file_path, 'w') as f2:
        for file_name in file_name_list:
            f2.write("file  " + file_name + "
") except Exception as e: raise Exception() #mp4 cmd = "ffmpeg -f concat -loglevel error -safe 0 -i " + temp_file_path + " -g 10 -s 640*340 -q 20 -c -copy " + merge_file_path try: proc = subprocess.Popen(cmd, shell=true, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, err = proc.communicate() except Exception as e: raise Exception() finally: if os.path.exists(temp_file_path): os.remove(temp_file_path)