adding stdout=devnull, stderr=devnull to python subprocess.call

master
acastro 8 years ago
parent 211890df88
commit 5d9f134d15

@ -1,8 +1,10 @@
#! /usr/bin/env python #! /usr/bin/env python
import re, subprocess, random import re, subprocess, random, os
from time import sleep from time import sleep
# requires: espeak to be installed devnull = open(os.devnull, 'w')
# requires: espeak and aplay (alsa-utils) to be installed
dic={ dic={
"narrator": "en-us", "narrator": "en-us",
@ -17,33 +19,25 @@ f=open("transcripts-drone-attack.txt","r")
txt=f.readlines() txt=f.readlines()
p= re.compile(r"^(\d\d\:\d\d) \((.*?)\)\: (.*)") # regex for capturing groups: time, character, sentence p= re.compile(r"^(\d\d\:\d\d) \((.*?)\)\: (.*)") # regex for capturing groups: time, character, sentence
for line in txt: for line in txt:
print line
if p.findall(line): if p.findall(line):
time,char,sentence = (p.findall(line))[0] time,char,sentence = (p.findall(line))[0]
print char.upper()
voice=dic[char] voice=dic[char]
# play time # play time
subprocess.call(["espeak", time +" "+char, "-v", dic['narrator'], "-p", "20"]) # narrator speaks: time and character subprocess.call(["espeak", time +" "+char, "-v", dic['narrator'], "-p", "20"], stdout=devnull, stderr=devnull) # narrator speaks: time and character
sleep(0.5) #short pause before sentence sleep(0.5) #short pause before sentence
print sentence
if "*expletive*" in sentence: #"*expletive*" in sentence is True: if "*expletive*" in sentence: #"*expletive*" in sentence is True:
sentence_parts=re.split(r"(\*\w+\*)", sentence) sentence_parts=re.split(r"(\*\w+\*)", sentence)
print sentence_parts
for part in sentence_parts: for part in sentence_parts:
if part == '*expletive*': if part == '*expletive*':
print 'EXPLETIVE', part subprocess.call(["aplay", 'swear.wav'], stdout=devnull, stderr=devnull)
subprocess.call(["aplay", 'swear.wav'])
else: else:
print 'SPEECH', part subprocess.call(["espeak", part, "-v", voice], stdout=devnull, stderr=devnull) # character speaks: his
subprocess.call(["espeak", part, "-v", voice]) # character speaks: his
sleep(float(random.randint(1,10))/100) sleep(float(random.randint(1,10))/100)
else: else:
subprocess.call(["espeak", sentence, "-v", voice]) # character speaks: his subprocess.call(["espeak", sentence, "-v", voice], stdout=devnull, stderr=devnull) # character speaks: his
# #
else: # line w/out time or character (narrator) else: # line w/out time or character (narrator)
print "NARRATOR" subprocess.call(["espeak", line, "-v", dic['narrator'], "-p", "20"], stdout=devnull, stderr=devnull)
subprocess.call(["espeak", line, "-v", dic['narrator'], "-p", "20"])
sleep(1) # make pause after each text line sleep(1) # make pause after each text line

Loading…
Cancel
Save