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深拷贝与浅拷贝用法,结合实例形式分析了Python对象的复制、深拷贝、浅拷贝等操作原理、用法及相关注意事项,需要的朋友可以参考下
    2019-05-05
  • Python的mysql数据库的更新如何实现

    Python的mysql数据库的更新如何实现

    这篇文章主要介绍了Python的mysql数据库的更新如何实现的相关资料,这里提供实例代码,帮助大家理解应用这部分知识,需要的朋友可以参考下
    2017-07-07
  • Python中如何生成GeoJSON数据

    Python中如何生成GeoJSON数据

    这篇文章主要介绍了Python中生成GeoJSON数据,无论使用geojson库还是geopandas库,都可以生成包含地理空间数据的GeoJSON文件,文中介绍了使用这些库生成GeoJSON数据的简单示例,需要的朋友可以参考下
    2023-10-10
  • Django集成Redis数据库的操作指南

    Django集成Redis数据库的操作指南

    本文将详细介绍如何在 Django 项目中集成 Redis 数据库,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • Python读取mp3中ID3信息的方法

    Python读取mp3中ID3信息的方法

    这篇文章主要介绍了Python读取mp3中ID3信息的方法,实例分析了Python中mutagen包的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • Python利用手势识别实现贪吃蛇游戏

    Python利用手势识别实现贪吃蛇游戏

    想必大家都玩过贪吃蛇的游戏吧:通过操纵蛇的移动方向能够让蛇吃到随机出现的食物,吃到的食物越多,蛇就会变得越长。本文将使用手势识别来完成贪吃蛇这个简单的游戏,感兴趣的可以了解一下
    2022-04-04
  • 详解python实现读取邮件数据并下载附件的实例

    详解python实现读取邮件数据并下载附件的实例

    这篇文章主要介绍了详解python读取邮件数据并下载附件的实例的相关资料,这里提供实现实例,帮助大家学习理解这部分内容,需要的朋友可以参考下
    2017-08-08
  • Python中sorted()排序与字母大小写的问题

    Python中sorted()排序与字母大小写的问题

    这篇文章主要介绍了Python中sorted()排序与字母大小写的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • 基于python+opencv调用电脑摄像头实现实时人脸眼睛以及微笑识别

    基于python+opencv调用电脑摄像头实现实时人脸眼睛以及微笑识别

    这篇文章主要为大家详细介绍了基于python+opencv调用电脑摄像头实现实时人脸眼睛以及微笑识别,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 关于Python两个列表进行全组合操作的三种方式

    关于Python两个列表进行全组合操作的三种方式

    这篇文章主要介绍了关于Python两个列表进行全组合操作的三种方式,两个元组 (a, b)(c, d),则它们的组合有 a,c a,d b,c b,d,这就叫全组合,需要的朋友可以参考下
    2023-04-04

最新评论