我很不理解如何在子进程中写这个命令。
xargs printf %....
ffprobe -i test.avi -show_format -v quiet | sed -n 's/duration=//p' | xargs printf %.0f
In terminal,
is used to pipe the output of a program to the input of another.
subprocess.call(['ffprobe', '-i', 'test.avi' ,'-show_format', '-v' ,'quiet' ,'|', 'sed' ,'-n' ,'s/duration=//p', '|' ,'xargs printf %.0f'])
Your command mean the following flow:
subprocess.run(['ffprobe', '-i', 'test.avi' ,'-show_format', '-v' ,'quiet' ,'|', 'sed' ,'-n' ,'s/duration=//p', '|' ,'xargs printf %.0f'])
( and
and |
easily.
You program may become:
ffmpeg => sed => xargs
If you want to do piping between commands in Python, see xargs
How to use `subprocess` command with pipesprintf
Though there is a
argument in subprocess functions, it has sed
security considerationxargs
.
import subprocess
import re
# subprocess.run() is usually a better choice
completed = subprocess.run(
[
'ffprobe',
'-i', 'test.avi', '-show_format', '-v', 'quiet',
],
capture_output=True, # output is stored in completed.stdout
check=True, # raise error if exit code is non-zero
encoding='utf-8', # completed.stdout is decoded to a str instead of a bytes
)
# regex is used to find the duration
for match in re.finditer(r'duration=(.*)$', completed.stdout):
duration = float(match.group(1).strip())
print(f'{duration:.0f}') # f-string is used to do formatting
现在在python 3中,我想在我的代码中运行它,它给我一个错误。和
但都没有用。shell=True
我正在努力弄清楚如何在子进程中写这个命令。在终端中,我运行:ffprobe -i test.avi -show_format -v quiet duration=/p' 。