Python3 ffmpeg视频转换工具使用方法解析

windows版本下需要先安装ffmpeg工具:

1:先下载指定(https://ffmpeg.zeranoe.com/builds/) 有Static,Shared,Dev三个版本,

可以下载了static版本(是个zip压缩包),解压到指定目录,去配置环境变量,比如d:\ffmpeg\bin,这样bin下面的ffmpeg.exe就可以在命令行中使用了,可以用ffmpeg -version测试一下:

代码如下:

def getImage(video_path):
  base_path = os.path.join(os.path.join(os.getcwd(), "mp4"), video_path)
  img_count = 1
  crop_time = 0.0
  try:
    while crop_time <= 22.0: #转化22s的视频
      os.system('D:\\ffmpeg-20191210-e73688e-win64-static\\bin\\ffmpeg -i %s -y -f image2 -ss %s %s.jpg'% (base_path, str(crop_time), str(img_count)))
      img_count += 1
      crop_time += 0.01 #每0.01秒截取一张照片
    print('视频转化完成!!!')
  except Exception as e:
    print(e)

def save():
  dir_s = os.path.join(os.getcwd(), "image")
  if os.path.exists(dir_s):
    shutil.rmtree(dir_s)
  os.mkdir(dir_s)
  for root, dirs, tmps in os.walk(os.getcwd()):
    for file in tmps:
      if file.endswith("jpg") and os.path.exists(os.path.join(dir_s, file)) is False:
        shutil.move(file, dir_s)

video_path = "of2.mp4"
getImage(video_path)
save()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持来客网。