如何将附件通过管道传递给变量

How to pipe attachment to variable

我想将 *.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:”

  • 子进程命令("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).