sed -n 's

问题描述 投票:0回答:1

我很不理解如何在子进程中写这个命令。

xargs printf %....

ffprobe -i test.avi -show_format -v quiet | sed -n 's/duration=//p' | xargs printf %.0fIn 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

runs
python-3.x subprocess ffprobe
1个回答
0
投票

and | easily.

You program may become:

ffmpeg => sed => xargsIf you want to do piping between commands in Python, see xargsHow to use `subprocess` command with pipesprintfThough there is a

argument in subprocess functions, it has sedsecurity 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' 。

© www.soinside.com 2019 - 2024. All rights reserved.