python操作音视频ffmpeg-python对比pyav选择

 更新时间:2023年11月28日 08:43:19   作者:ponponon  
这篇文章主要介绍了python操作音视频的选择:ffmpeg-python对比pyav,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

ffmpeg 音视频操作

ffmpeg 是音视频领域的王者,对音视频的操作,离不开 ffmpeg

在 python 生态下面使用 ffmpeg 有两个著名的库

fmpeg-python 对比 pyav

那推荐用哪个呢?

当然是后者:pyav

为什么?他两有什么区别?

那就是调用 ffmpeg 的方式不同

ffmpeg-python 是直接调用 ffmpeg 这个可执行程序来操作音视频的,这就要求你本地安装 ffmpeg。而且每次操作,都相当于是起了一个 ffmpeg进程,非常的低效。

而 pyav 是链接了 ffmpeg 的动态链接库 libav,所以不存在每次操作都启动一个 ffmpeg 进程的问题,更加高效优雅

使用 ffmpeg-python,如果本地没有安装 ffmpeg,就会报错如下:

In [1]: import ffmpeg
   ...: stream = ffmpeg.input('input.mp4')
   ...: stream = ffmpeg.hflip(stream)
   ...: stream = ffmpeg.output(stream, 'output.mp4')
   ...: ffmpeg.run(stream)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[1], line 5
      3 stream = ffmpeg.hflip(stream)
      4 stream = ffmpeg.output(stream, 'output.mp4')
----> 5 ffmpeg.run(stream)

File /usr/local/lib/python3.11/site-packages/ffmpeg/_run.py:313, in run(stream_spec, cmd, capture_stdout, capture_stderr, input, quiet, overwrite_output)
    289 @output_operator()
    290 def run(
    291     stream_spec,
   (...)
    297     overwrite_output=False,
    298 ):
    299     """Invoke ffmpeg for the supplied node graph.
    300 
    301     Args:
   (...)
    311     Returns: (out, err) tuple containing captured stdout and stderr data.
    312     """
--> 313     process = run_async(
    314         stream_spec,
    315         cmd,
    316         pipe_stdin=input is not None,
    317         pipe_stdout=capture_stdout,
    318         pipe_stderr=capture_stderr,
    319         quiet=quiet,
    320         overwrite_output=overwrite_output,
    321     )
    322     out, err = process.communicate(input)
    323     retcode = process.poll()

File /usr/local/lib/python3.11/site-packages/ffmpeg/_run.py:284, in run_async(stream_spec, cmd, pipe_stdin, pipe_stdout, pipe_stderr, quiet, overwrite_output)
    282 stdout_stream = subprocess.PIPE if pipe_stdout or quiet else None
    283 stderr_stream = subprocess.PIPE if pipe_stderr or quiet else None
--> 284 return subprocess.Popen(
    285     args, stdin=stdin_stream, stdout=stdout_stream, stderr=stderr_stream
    286 )

File /usr/local/lib/python3.11/subprocess.py:1026, in Popen.__init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask, pipesize, process_group)
   1022         if self.text_mode:
   1023             self.stderr = io.TextIOWrapper(self.stderr,
   1024                     encoding=encoding, errors=errors)
-> 1026     self._execute_child(args, executable, preexec_fn, close_fds,
   1027                         pass_fds, cwd, env,
   1028                         startupinfo, creationflags, shell,
   1029                         p2cread, p2cwrite,
   1030                         c2pread, c2pwrite,
   1031                         errread, errwrite,
   1032                         restore_signals,
   1033                         gid, gids, uid, umask,
   1034                         start_new_session, process_group)
   1035 except:
   1036     # Cleanup if the child failed starting.
   1037     for f in filter(None, (self.stdin, self.stdout, self.stderr)):

File /usr/local/lib/python3.11/subprocess.py:1950, in Popen._execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, gid, gids, uid, umask, start_new_session, process_group)
   1948     if errno_num != 0:
   1949         err_msg = os.strerror(errno_num)
-> 1950     raise child_exception_type(errno_num, err_msg, err_filename)
   1951 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg'

以上就是python 操作音视频的选择:ffmpeg-python 对比 pyav的详细内容,更多关于python mpeg-python对比pyav的资料请关注脚本之家其它相关文章!

相关文章

  • python实现水印生成器

    python实现水印生成器

    这篇文章主要为大家详细介绍了python实现水印生成器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 如何理解python面向对象编程

    如何理解python面向对象编程

    在本篇文章里小编给大家整理的是关于如何理解python面向对象编程的相关知识点,需要的朋友们可以学习下。
    2020-06-06
  • 树莓派+摄像头实现对移动物体的检测

    树莓派+摄像头实现对移动物体的检测

    这篇文章主要为大家详细介绍了树莓派+摄像头实现对移动物体的检测,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • python GUI库图形界面开发之PyQt5信号与槽基本操作

    python GUI库图形界面开发之PyQt5信号与槽基本操作

    这篇文章主要介绍了python GUI库图形界面开发之PyQt5信号与槽基本操作,需要的朋友可以参考下
    2020-02-02
  • 利用Python 制作二维码

    利用Python 制作二维码

    这篇文章主要介绍的是如何利用Python 制作二维码,文章从介绍python 二维码制作的第三方库QRCode 和MyQR展开话题,需要的小伙伴可以参考一下文章的具体内容
    2021-09-09
  • Python制作摩斯密码翻译器

    Python制作摩斯密码翻译器

    摩斯密码是一种将文本信息作为一系列通断的音调、灯光或咔嗒声传输的方法,本文将介绍如何通过Python制作摩斯密码翻译器,感兴趣的童鞋可以关注一下
    2021-11-11
  • Python实现功能全面的学生管理系统

    Python实现功能全面的学生管理系统

    这篇文章主要为大家详细介绍了Python实现功能全面的学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • python实现弹跳小球

    python实现弹跳小球

    这篇文章主要为大家详细介绍了python实现弹跳小球,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • 解决jupyter notebook启动后没有token的坑

    解决jupyter notebook启动后没有token的坑

    这篇文章主要介绍了解决jupyter notebook启动后没有token的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • 更新pip3与pyttsx3文字语音转换的实现方法

    更新pip3与pyttsx3文字语音转换的实现方法

    今天小编就为大家分享一篇更新pip3与pyttsx3文字语音转换的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08

最新评论