簡體   English   中英

播放時Python Mplayer腳本無法運行

[英]Python Mplayer Script Doesn't Run While Playing

我對Python和Linux命令非常陌生,但是我想盡全力以赴。

我的應用程序大於此示例,但在擴展之前,我試圖使其正常運行。

我在從屬模式下使用mplayer播放視頻剪輯,但是我想在剪輯完成后自動將其殺死。 所以我的想法是在從屬模式下啟動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。

謝謝!

您可以按照有關os.popen的文檔中的建議使用進程lib( 從2.6版開始推薦使用):該功能已過時 ,您所需要做的就是使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM