使用Python的popen

时间:2018-04-10 02:41:33

标签: python ffmpeg popen

我有一个Python 3.6程序,它使用ffplay命令从ffmpeg库调用Popen

我想发送ffplay次按键,以便控制播放。但似乎没有任何事情发生。举个例子:

import subprocess
import time

# Array with the command in it
my_command = ("ffplay", "-nodisp", "-autoexit", "examples/sounds/Fantascape_Looping.mp3")

# Open a subprocess
my_subprocess = subprocess.Popen(my_command, 
                                 stdin=subprocess.PIPE, 
                                 stdout=subprocess.PIPE, 
                                 stderr=subprocess.PIPE)

# Wait 2 seconds
time.sleep(2)

# Try to send an escape:
print("Attempt to send and 'escape' to stop the sound")
my_subprocess.stdin.write(b'\x1b')

# Wait and see what happens
print("Should no longer hear anything")
time.sleep(5)

我听到音乐的时间是7秒,而不是2秒。代码有什么问题?

1 个答案:

答案 0 :(得分:1)

向附加ffplay的{​​{1}}进程发送转义击键将无法执行任何操作。要验证这一点,请执行-nodisp并尝试按下转义键。它显然注册,但程序并不关心。所以这种行为是可以预料的。

此外,所有按键都由GUI注册ffplay -nodisp examples/sounds/Fantascape_Looping.mp3,因此您传入的任何内容都不重要(禁止在Unix系统上使用Control-C等默认设置)。

要解决您的问题,您需要避免尝试将击键发送到ffplay。其中一个(相当粗略的)解决方案是直接终止进程,但这显然只能用于停止播放。

ffplay

除了this mailing list question之外,我找不到任何关于控制my_subprocess.terminate() # replacing your writing to stdin 播放的内容,除了https://laravel.com/docs/5.5/authentication#protecting-routes之外还没有答案,它会询问以下内容。

  

是否有办法让ffplay接受播放控制   终端而不是播放器窗口?与ffmeg的相似之处   可以接受stdin输入来控制过滤器。

这让我相信你可能想要在StackOverflow上没有ffplay的显示来询问有关播放控制的另一个问题(我没有答案),或者使用不同的库。< / p>

由于您无法控制ffplay -novid进程,我可以考虑的唯一合理解决方案是限制停止播放问题的直接范围。

相关问题