2014-09-23 61 views
1

(这是一个运行2014-09-09-wheezy-raspbian的Raspberry Pi B,更新和升级已经运行,mplayer已经安装并经过测试,使用Python编码3)在Raspberry Pi上使用Python 3,如何调用MPlayer并传递一个URL

我只涉足Linux和Pi,所以我来专家指导。

让我说,这个工程启动: 须藤./speech.sh鲍勃是你的叔叔

的皮愉快地去了谷歌和:

#!/bin/bash 
say() { local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?tl=en&q=$*"; } 
# say() { local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.co.uk/translate_tts?tl=en&q=$*"; } 

say $* 

这个bash脚本是像这样执行通过mplayer播放TTS。这让我知道我的叔叔是谁。

我的问题发生在Python中。

我的最终目标是在启动时启动一个小型python 3脚本,并且每隔一段时间都会说一些随机语句。这将被安置在一个陶瓷头骨的内部,为附近的幽灵和食尸鬼所喜爱。

这是我的python。希望有人能告诉我我做错了什么?

import urllib, os, random, urllib.request, urllib.parse, shutil, subprocess 

def getGoogleSpeechURL(phrase): 
    print (phrase) 
    googleURL = "http://translate.google.com/translate_tts?tl=en&" 
    parameters = {'q': phrase} 
    data = urllib.parse.urlencode(parameters) 
    googleURL = "%s%s" % (googleURL, data) 
    return googleURL 

def random_line(afileName): 
    with open(afileName, "r") as afile: 
     line = next(afile) 
     for num, aline in enumerate(afile): 
      if random.randrange(num + 2): continue 
      line = aline 
     return line 

def speakSpeechFromText(phrase): 
    print (phrase) 
    googleSpeechURL = getGoogleSpeechURL(phrase) 
    commandString = '/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols ' + googleSpeechURL 
    print (commandString) 
    os.system(commandString) 


filePath = "/home/pi/TalkingSkullPhrases.txt" 

speakSpeechFromText (random_line(filePath)) 

我已经尝试过os.system,subprocess,Popen ...并没有任何工作。系统调用只是打印命令字符串,然后退出(是在mplayer之前结束的代码?如果是的话,你如何让它等待?)。子进程和Popen都抱怨说找不到mplayer(我试过完全合格的路径,没有运气)。

所以*尼克斯和蟒蛇大师,关心指出我是如何成为一个白痴,我如何解决它。 :-)

+1

当你使用'subprocess'时,你是否把命令分解成一个列表?例如。 'subprocess.call(['ls -l'])'失败,但'subprocess.call(['ls','-l'])按预期工作。 – Marius 2014-09-23 23:51:47

+0

这样的作品,像 subprocess.call([ 'mplayer的', '-ao', 'ALSA', '-really平静', '-noconsolecontrols',googleSpeechURL]) 但我不能标记您评论作为答案,因为它不会让我。 – 2014-09-24 00:40:01

+0

是的,评论不是真的应该为答案,但我只是在你遇到的问题上进行一个有根据的猜测,现在会写出来。 – Marius 2014-09-24 00:45:41

回答

2

使用subprocess时出现的一个常见错误是它需要在列表中提供参数,而不是单个字符串(除非使用shell=True等一些选项)。所以你的mplayer电话应该是:

subprocess.call(['mplayer', '-ao', 'alsa', '-really-quiet', '-noconsolecontrols', googleSpeechURL]) 
相关问题