gpt4 book ai didi

python - 如何通过管道连接到变量

转载 作者:行者123 更新时间:2023-12-04 22:54:26 25 4
gpt4 key购买 nike

我想将 *.mka 内部的附件通过管道传输到变量而不是文件。
将附件保存到 *.txt 文件如下所示(并且有效):

import io
import subprocess
import sys, os
import soundfile as sf

full_path = os.path.realpath(__file__)
path, filex = os.path.split(full_path)

fname = 'my_own_soundfile.mka'
fullpath = path + '\\' + fname
pathffmpeg = 'C:/FFmpeg/bin/ffmpeg.exe'

command = [pathffmpeg, "-dump_attachment:t:0",
"C:\folder\\attachment.txt",
"-i", fullpath] # This one works

proc = subprocess.run(command, stdout=subprocess.PIPE)
这个命令是我尝试管道附件,但是这不起作用:
command = [pathffmpeg, "-dump_attachment:t:0",
"-f", "PIPE:0",
"-i", fullpath]
我能做些什么来完成这项工作?

最佳答案

对于管道标准输出内容,将输出文件名参数替换为“管道:”

  • 子进程命令( "pipe:1""pipe:" ):
     command = [pathffmpeg, "-dump_attachment:t:0",
    "pipe:1",
    "-i", fullpath]
  • 阅读 sdtout内容,添加.stdout运行命令后:
     attachment = subprocess.run(command, stdout=subprocess.PIPE).stdout

  • 结果格式为字节数组。

    这是一个完整的代码示例:
    import subprocess

    fname = 'my_own_soundfile.mka'

    # For piping stdout content, replace output file name argument with "pipe:".
    command = ["ffmpeg", "-dump_attachment:t:0", "pipe:", "-i", fname]

    # Execute FFmpeg as subprocess and read from stdout pipe (attachment is "bytes array").
    attachment = subprocess.run(command, stdout=subprocess.PIPE).stdout

    attachment_str = attachment.decode("utf-8") # Convert from bytes array to string (converting to string is optional).

    关于python - 如何通过管道连接到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67772285/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com