2012-07-08 53 views
0

我試圖與applescript一起檢索歌曲的BPM值。最終我想用遊戲來實現它。這裏是我的代碼:Python osascript返回0它似乎

import os 
import time 
import sys 


def getBPM(): 
    iTunesInstruct = """' 
    tell application "iTunes" 
    set k to get bpm of current track 
    end tell 
    return k 
    '""" 
    bpm = os.system('arch -i386 osascript -e ' + iTunesInstruct) 
    #bpm =90 

    bpm = int(bpm) 
    bpm = round(bpm) 

    if bpm > 250: 
     bpm = 200 
    return bpm 


def getBeatSecond(bpm): 
    bps = float(bpm)/60 
    #raw_input(bps) 
    return float(bps) 

i = 0 

beatMatch = True 

while True: 
    beat = 1/getBeatSecond(getBPM()) # BPS Beat divided by a second. 

    if beatMatch: 
     time.sleep(beat) 
     print beat 
    else: 
     raw_input('Go??') 
    i += 1 
    if i > 50: 
     break 

但這似乎只能使用一次......它讓我在聽歌曲的BPM,看到是94,然後似乎就可以認爲這是第二次迭代0,然後它除以0並死亡。這是怎麼回事?

回答

1

os.system不等待命令完成。

=是osascript的結果, =沒有故障,則退出狀態(使用os.system)。

使用subprocess.Popen

from subprocess import Popen, PIPE 

def getBPM(): 
    cmd = "arch -i386 osascript -e " + """'tell application "iTunes" to return bpm of current track'""" 
    bpm, tError = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate() 
    if bpm > 250: 
     return = 200 
    return int(bpm) 
+0

嘿嘿,謝謝,我不知道什麼管道是一回事,或者是任何的意思,但我只是想它和它仍然返回0 .... :( – user1159454 2012-07-09 08:27:22

+0

我不好,這是有效的,只是這首歌沒有BPM,我加了一個嘗試,謝謝! – user1159454 2012-07-09 09:49:59

相關問題