2014-01-29 215 views
2

我已經寫了這段代碼,所以當我輸入一些信息時,它會創建一個代碼讓我可以直播到抽搐。這是代碼:爲什麼我得到這個錯誤

import time 
ip = input("Please enter the local IP address: ") 
port = input("Please enter the port number: ") 
resolution = input("Please enter the resolution: ") 
valid_classes = ["ultrafast", "fast", "medium", "slow"] 
while True: 
    print('What preset would you like? Choose from ultrafast, fast, medium and slow: ') 
    preset1 = input() 
    if preset1 not in valid_classes: 
     print("Please Enter A Valid Preset All In Lower Case") 
     time.sleep(1) 
    else: 
     fps = input("How many FPS would you like: ") 
     valid_service = ["youtube", "twitch"] 
     while True: 
      print('What service would you like to stream to? Please choose from youtube or twitch: ') 
      service = input() 
      if service not in valid_service: 
       print("Please Enter A Valid Service All In Lower Case") 
       time.sleep(1) 
      else: 
       key = input("Please enter your stream key: ") 
       if service == "twitch": 
        fortwitch = ("rtmp://live.justin.tv/app/")+key+("") 
        print("ffmpeg -f mpegts -i 'udp://")+ip+(":")+port+("?fifo_size=1000000&buffer_size=10000000&overrun_nonfatal=1' -vcodec libx264 -s ")+resolution+(" -g:v 40 -qscale:v 2 -threads 4 -preset ")+preset1+(" -b:v 1136k -minrate 1300k -maxrate 1300k -bufsize 8000k -r ")+fps+(" -acodec libmp3lame -ar 44100 -b:a 64k -f flv ")+fortwitch+("") 
       else: 
        print ("ok") 

當我運行代碼,我得到了以下錯誤消息:

Traceback (most recent call last): 
    File "/Users/kiancross/Desktop/LIVESTREAM/livestreamoptions.py", line 25, in <module> 
    print("ffmpeg -f mpegts -i 'udp://")+ip+(":")+port+("?  fifo_size=1000000&buffer_size=10000000&overrun_nonfatal=1' -vcodec libx264 -s ")+resolution+(" -g:v 40 -qscale:v 2 -threads 4 -preset ")+preset1+(" -b:v 1136k -minrate 1300k -maxrate 1300k -bufsize 8000k -r ")+fps+(" -acodec libmp3lame -ar 44100 -b:a 64k -f flv ")+fortwitch+("") 
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' 

我不知道我做得更多。有關我在代碼中所做的更多信息,請參閱:http://marctv.de/blog/tutorial-hd-pvr-twitch-mac-os-ffmpeg/

此外,我如何使它在終端創建的代碼自動運行在終端中(我在Mac上)。

編輯

當我運行你的新代碼我做對了嗎?

import time 
ip = input("Please enter the local IP address: ") 
port = input("Please enter the port number: ") 
resolution = input("Please enter the resolution: ") 
valid_classes = ["ultrafast", "fast", "medium", "slow"] 
while True: 
    print('What preset would you like? Choose from ultrafast, fast, medium and slow: ') 
    preset1 = input() 
    if preset1 not in valid_classes: 
     print("Please Enter A Valid Preset All In Lower Case") 
     time.sleep(1) 
    else: 
     fps = input("How many FPS would you like: ") 
     valid_service = ["youtube", "twitch"] 
     while True: 
      print('What service would you like to stream to? Please choose from youtube or twitch: ') 
      service = input() 
      if service not in valid_service: 
      print("Please Enter A Valid Service All In Lower Case") 
      time.sleep(1) 
      else: 
       key = input("Please enter your stream key: ") 
       if service == "twitch": 
        fortwitch = ("rtmp://live.justin.tv/app/")+key+("") 
        args = [ 
    'ffmpeg', '-f', 'mpegts', '-i', 
    'udp://{}:{}?fifo_size=1000000&buffer_size=10000000&overrun_nonfatal=1'.format(ip, port), 
    '-vcodec', 'libx264', '-s', resolution, '-g:v', '40', '-qscale:v', '2', 
    '-threads', '4', '-preset', preset1, '-b:v', '1136k', '-minrate', '1300k', 
    '-maxrate', '1300k', '-bufsize', '8000k', '-r', fps, '-acodec', 'libmp3lame', 
    '-ar', '44100', '-b:a', '64k', '-f', 'flv', fortwitch] 

subprocess.call(args) 

當我運行它只是循環回到你想要流到什麼樣的服務。當它在終端它只是說

Traceback (most recent call last): 
    File "./livestreamoptions.py", line 8, in <module> 
    preset1 = input() 
    File "<string>", line 1, in <module> 

回答

4

print()回報None和你正在嘗試更多的字符串添加到它:

print("ffmpeg -f mpegts -i 'udp://")+ip # etc. 
# ---------------- print ends here^

將你的整個字符串連接print()功能:

print("ffmpeg -f mpegts -i 'udp://"+ip+":"+port+"?fifo_size=1000000&buffer_size=10000000&overrun_nonfatal=1' -vcodec libx264 -s "+resolution+" -g:v 40 -qscale:v 2 -threads 4 -preset "+preset1+" -b:v 1136k -minrate 1300k -maxrate 1300k -bufsize 8000k -r "+fps+" -acodec libmp3lame -ar 44100 -b:a 64k -f flv "+fortwitch) 

還是更好,用字符串格式代替:

print("ffmpeg -f mpegts -i 'udp://{}:{}?fifo_size=1000000&buffer_size=10000000&overrun_nonfatal=1' " 
     "-vcodec libx264 -s{} -g:v 40 -qscale:v 2 -threads 4 -preset {} " 
     "-b:v 1136k -minrate 1300k -maxrate 1300k -bufsize 8000k -r {} " 
     " -acodec libmp3lame -ar 44100 -b:a 64k -f flv {}".format(
     ip, port, resolution, preset1, fps, fortwitch)) 

它具有更多的可讀性的額外優勢。

要真正執行該命令,使用subprocess module,但你使用的參數列表,而不是會更好:

args = [ 
    'ffmpeg', '-f', 'mpegts', '-i', 
    'udp://{}:{}?fifo_size=1000000&buffer_size=10000000&overrun_nonfatal=1'.format(ip, port), 
    '-vcodec', 'libx264', '-s', resolution, '-g:v', '40', '-qscale:v', '2', 
    '-threads', '4', '-preset', preset1, '-b:v', '1136k', '-minrate', '1300k', 
    '-maxrate', '1300k', '-bufsize', '8000k', '-r', fps, '-acodec', 'libmp3lame', 
    '-ar', '44100', '-b:a', '64k', '-f', 'flv', fortwitch] 

subprocess.call(args) 

它可以讓你只列出你的論點作爲args單獨的元素列表,最小字符串格式。

相關問題