ffmpeg 是音视频领域的王者,对音视频的操作,离不开 ffmpeg
在 python 生态下面使用 ffmpeg 有两个著名的库
那推荐用哪个呢?
当然是后者: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'
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。