2016-06-23 20 views
0

我非常喜欢Python和Linux命令的noob,但我试图围绕它来包裹我的头。Python Mplayer脚本在播放时不运行

我的应用程序比这个示例大,但我想在扩展之前得到这个工作。

我在从模式下使用mplayer播放视频剪辑,但我想在剪辑完成时自动将其终止。所以我的想法是在slave模式下启动mplayer,然后命令'get_time_length',存储该值,为该值睡眠,杀死它并返回主例程。

import os 
import commands 
import glob 

#Global Commands 
glcmd = 'get_time_length' 

print("Preparing for kiosk mode! Press CTRL+C to exit") 
try: 
    while 1:   
     path = '/media/*/button1/*' 
     os.system('mplayer -fs -quiet -slave path') 
     line = os.popen(glcmd).readline().strip() #Get the duration from mplayer 
     duration = line.split('=')[1] #Get just the length of video in Seconds 
     print(duration) 
     sleep(duration) #Wait until the video is done 
     os.system('quit') 

,我遇到的问题是,一旦视频片段开始播放,它永远不会在播放过程中运行的任何命令。我也不确定我的语法是否正确,以便检索命令行,但我至少想知道如何在播放视频时通过脚本向mplayer发送命令。

谢谢!

回答

0

您可以使用subprocess LIB在其中说,自从2.6版本约os.popen 弃用的docs建议:这个函数是废弃,所有你需要做的是使用check_call程序时,该视频确实将结束。

from subprocess import check_call 

path = '/media/*/button1/*' 
check_call(['mplayer',"-fs", '-quiet' ,'-slave',path]) 

如果你想以某种方式,你会用subprocess.Popen沟通,这将打印所有输出在一个循环,如果用户输入CTRL-C终止进程:从时间导入睡眠 从子

进口POPEN,PIPE

path = '/media/*/button1/*' 
try: 
    p = Popen(['mplayer', '-quiet' ,'-slave', path],stdout=PIPE, stdin=PIPE) 
    for line in iter(p.stdout.readline,""): 
     print(line) 
except KeyboardInterrupt: 
    p.terminate() 

如果你想发送的命令,你会写信给p.stdin