我试图在不带Shell = true的情况下运行subprocess.popen,但无法弄清楚如何格式化列表。它始终会失败,并带有无效的url或403。就像它没有设置cookie或标头一样。
示例网址
https://vodus-i-video-xxx.xxx.net/vodus/1d9cfcbc-8469-4c9f-b44a-6412091b733d/NoADs_IND_SEA_720_pre1.ism/QualityLevels(4740905)/Manifest(video,format=m3u8-aapl-v3,audiotrack=aac_UND_2_127,filter=chromecast)?hdnea=st=1535729260~exp=1535729440~acl=/*~hmac=977cba9e480bcdb7b89033488f37679b4a6906f8f7a981a055b88a0802012c71&hdcore=2.11.3|Connection=keep-alive&User-Agent=Firefox|Connection=keep-alive&User-Agent=Firefox
我解析网址以获取Cookie:
"hdnea=st=1535729260~exp=1535729440~acl=/*~hmac=977cba9e480bcdb7b89033488f37679b4a6906f8f7a981a055b88a0802012c71&hdcore=2.11.3;path=/; domain=livemain-i-video-xxx.xxx.net;86400s"
将与Shell = true一起使用的最终字符串
streamlink --player-external-http --player-external-http-port 10000 --http-header "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0" --http-cookie "hdnea=st=1535729260~exp=1535729440~acl=/*~hmac=977cba9e480bcdb7b89033488f37679b4a6906f8f7a981a055b88a0802012c71&hdcore=2.11.3;path=/; domain=livemain-i-video-xxx.xxx.net;86400s" "hlsvariant://https://vodus-i-video-xxx.xxx.net/vodus/1d9cfcbc-8469-4c9f-b44a-6412091b733d/NoADs_IND_SEA_720_pre1.ism/QualityLevels(4740905)/Manifest(video,format=m3u8-aapl-v3,audiotrack=aac_UND_2_127,filter=chromecast)?hdnea=st=1535729260~exp=1535729440~acl=/*~hmac=977cba9e480bcdb7b89033488f37679b4a6906f8f7a981a055b88a0802012c71&hdcore=2.11.3|Connection=keep-alive&User-Agent=Firefox|Connection=keep-alive&User-Agent=Firefox" best
这是我尝试格式化的方法,但似乎无法正确处理。
stream_url="https://vodus-i-video-nfl.akamaized.net/vodus/1d9cfcbc-8469-4c9f-b44a-6412091b733d/NoADs_IND_SEA_720_pre1.ism/QualityLevels(4740905)/Manifest(video,format=m3u8-aapl-v3,audiotrack=aac_UND_2_127,filter=chromecast)?hdnea=st=1535729260~exp=1535729440~acl=/*~hmac=977cba9e480bcdb7b89033488f37679b4a6906f8f7a981a055b88a0802012c71&hdcore=2.11.3|Connection=keep-alive&User-Agent=Firefox|Connection=keep-alive&User-Agent=Firefox"
urlsplit = stream_url.split('?')
getcookie = urlsplit[1].split('|')
cookie = getcookie[0]
cmd = [
"streamlink",
"--player-external-http",
"--player-external-http-port",
"%s" %(port),
"--http-header",
"\"User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0\"",
"--http-cookie",
"\"%s" %(cookie) + ";path=/;domain=livemain-i-video-nfl.akamaized.net;86400s\"",
"%s" %(streamtest),
"best"
]
logger.info("Running cmd: %s" % " ".join(cmd))
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
logging.info(output)
我得到的输出是:
INFO:root:Running cmd:streamlink --player-external-http --player-external-http-port 15000 --http-header“ User-Agent = Mozilla / 5.0(Windows NT 10.0; Win64; x64; rv:61.0)Gecko / 20100101 Firefox / 61.0“ --http-cookie” hdnea = st = 1535680989〜exp = 1535681169〜acl = / *〜hmac = 0232a24cb481541387ff7984e7b78f44f6ccdf3d2893d8bbbe4eb7e44fe1baea&hdcore = -2.11.3; live = video-xxx.xxx.net; 86400s“ hlsvariant:// https://livemain-i-video-xxx.xxx.net/ch16/7fd8a3f6-5dee-4ed1-b23f-7398684ff5bd/7fd8a3f6-5dee-4ed1-b23f-7398684ff5bd.ism/QualityLevels(2200000)/Manifest(video,format=m3u8-aapl-v3,audiotrack=audio,filter=chromecast-live)最佳
INFO:root:[cli] [info]流链接以root身份运行!小心! [cli] [info]找到了URL hlsvariant:// https://livemain-i-video-xxx.xxx.net/ch16/7fd8a3f6-5dee-4ed1-b23f-7398684ff5bd/7fd8a3f6-5dee-4ed1-b23f-7398684ff5bd.ism/QualityLevels(2200000)/Manifest(video,format=m3u8-aapl-v3,audiotrack=audio,filter=chromecast-live)的匹配插件hls 错误:无法打开URL:https://livemain-i-video-xxx.xxx.net/ch16/7fd8a3f6-5dee-4ed1-b23f-7398684ff5bd/7fd8a3f6-5dee-4ed1-b23f-7398684ff5bd.ism/QualityLevels(2200000)/Manifest(video,format=m3u8-aapl-v3,audiotrack=audio,filter=chromecast-live)(403客户端错误:禁止URL:https://livemain-i-video-xxx.xxx.net/ch16/7fd8a3f6-5dee-4ed1-b23f-7398684ff5bd/7fd8a3f6-5dee-4ed1-b23f-7398684ff5bd.ism/QualityLevels(2200000)/Manifest(video,format=m3u8-aapl-v3,audiotrack=audio,filter=chromecast-live))
联接的作用看起来是正确的,但似乎无法正确执行。我确实认为由于特殊字符,我需要对Cookie和url加上双引号。只是希望得到一个输出,而在Shell = True的情况下,直到完成它才得到输出。
答案 0 :(得分:0)
User-Agent标头和cookie周围的文字引号看起来可疑。我猜你想要
cmd = [
"streamlink",
"--player-external-http",
"--player-external-http-port",
str(port),
"--http-header",
"User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0",
"--http-cookie",
"%s;path=/;domain=livemain-i-video-nfl.akamaized.net;86400s"%(cookie),
streamtest,
"best"
]
还请注意我们如何避免使用%
字符串格式将某些内容转换为已经是 字符串的字符串。