1. 程式人生 > >python 呼叫ffmpeg轉html5支援的mp4

python 呼叫ffmpeg轉html5支援的mp4

 

 

# coding: utf-8
import sys, os
import threading


def convert_avi(input_file, output_file, ffmpeg_exec="ffmpeg"):
   ffmpeg = '{ffmpeg} -y -i "{infile}" -c:v libx264 -strict -2 "{outfile}"'.format(ffmpeg=ffmpeg_exec,    infile=input_file,outfile=output_file
   )

   f = os.popen(ffmpeg)
   ffmpegresult = f.readline()

   #s = os.stat(output_file)
   #fsize = s.st_size

   return ffmpegresult

def convert_avi_to_webm(input_file, output_file, ffmpeg_exec="ffmpeg"):
   return convert_avi(input_file, output_file, ffmpeg_exec="ffmpeg")

def convert_avi_to_mp4(input_file, output_file, ffmpeg_exec="ffmpeg"):
   return convert_avi(input_file, output_file, ffmpeg_exec="ffmpeg")

def convert_to_avcmp4(input_file, output_file, ffmpeg_exec="ffmpeg"):
   email = threading.Thread(target=convert_avi, args=(input_file, output_file, ffmpeg_exec,))
   email.start()


def convert_byfile(from_path, to_path):
   if not os.path.exists(from_path):
      print("Sorry, you must create the directory for the output files first")
   if not os.path.exists(os.path.dirname(to_path)):
      os.makedirs(os.path.dirname(to_path),exist_ok=True)
   directory, file_name = os.path.split(from_path)
   raw_name, extension = os.path.splitext(file_name)
   print("Converting ", from_path)
   convert_avi_to_mp4(from_path, to_path)



if __name__ == "__main__":
   from_path=r'D:\project\hik_client_dll\x64\Debug\3333.mp4'
   to_path=r'D:\project\hik_client_dll\x64\Debug\1111.mp4'
   res= convert_byfile(from_path, to_path)
   print(res)