gpt4 book ai didi

python - 为什么会出现此错误

转载 作者:行者123 更新时间:2023-11-28 22:51:31 26 4
gpt4 key购买 nike

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

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>

最佳答案

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

关于python - 为什么会出现此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21440224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com