为什么我会收到此错误

时间:2014-01-29 19:03:02

标签: python python-3.x

我已经编写了这段代码,这样当我输入一些信息时,它会创建代码,让我可以直播到抽搐。这是代码:

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>

1 个答案:

答案 0 :(得分: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列表中的单独元素,并使用最少的字符串格式。