feat: optimize

This commit is contained in:
abc1763613206
2021-02-15 14:05:33 +08:00
parent 1e607e89c7
commit b724f290d7
3 changed files with 303 additions and 299 deletions

70
main.py
View File

@@ -6,11 +6,13 @@ import re
import traceback
import requests
import subprocess
import time
from ffmpy import FFprobe
from subprocess import PIPE
from sys import stdout
from termcolor import colored, RESET
from datetime import datetime
from func_timeout import func_set_timeout, FunctionTimedOut
dt=datetime.now()
# Channel Group Source Link
@@ -20,6 +22,17 @@ SKIP_FFPROBE_MESSAGES = [re.compile(pattern) for pattern in (
'number of reference frames .+ exceeds max',
)]
@func_set_timeout(12)
def get_stream(num, clist, uri):
try:
ffprobe = FFprobe(inputs={uri: '-v error -show_format -show_streams -print_format json'})
cdata = json.loads(ffprobe.run(stdout=PIPE,stderr=PIPE)[0].decode('utf-8'))
return cdata
except Exception as e:
#traceback.print_exc()
print('[{}] {}({}) Error:{}'.format(str(num), clist[0], clist[2], str(e)))
return False
def check_channel(clist,num):
# clist 为一行 csv
uri = clist[3]
@@ -35,28 +48,30 @@ def check_channel(clist,num):
# print('[{}] {}({}) Error:{}'.format(str(num), clist[0], clist[2], str(errors)))
# return False
#else: # 查视频信息
ffprobe = FFprobe(inputs={uri: '-v error -show_format -show_streams -print_format json'})
cdata = json.loads(ffprobe.run(stdout=PIPE,stderr=PIPE)[0].decode('utf-8'))
flagAudio = 0
flagVideo = 0
vwidth = 0
vheight = 0
for i in cdata['streams']:
if i['codec_type'] == 'video':
flagVideo = 1
if vwidth <= i['coded_width']: # 取最高分辨率
vwidth = i['coded_width']
vheight = i['coded_height']
elif i['codec_type'] == 'audio':
flagAudio = 1
if flagAudio == 0:
print('[{}] {}({}) Error: Video Only!'.format(str(num), clist[0], clist[2]))
cdata = get_stream(num, clist, uri)
if cdata:
flagAudio = 0
flagVideo = 0
vwidth = 0
vheight = 0
for i in cdata['streams']:
if i['codec_type'] == 'video':
flagVideo = 1
if vwidth <= i['coded_width']: # 取最高分辨率
vwidth = i['coded_width']
vheight = i['coded_height']
elif i['codec_type'] == 'audio':
flagAudio = 1
if flagAudio == 0:
print('[{}] {}({}) Error: Video Only!'.format(str(num), clist[0], clist[2]))
return False
if flagVideo == 0:
print('[{}] {}({}) Error: Audio Only!'.format(str(num), clist[0], clist[2]))
return False
print('[{}] {}({}) PASS: {}*{}'.format(str(num), clist[0], clist[2], vwidth, vheight))
return [vwidth,vheight]
else:
return False
if flagVideo == 0:
print('[{}] {}({}) Error: Audio Only!'.format(str(num), clist[0], clist[2]))
return False
print('[{}] {}({}) PASS: {}*{}'.format(str(num), clist[0], clist[2], vwidth, vheight))
return [vwidth,vheight]
else:
print('[{}] {}({}) Error:{}'.format(str(num), clist[0], clist[2], str(r.status_code)))
return False
@@ -73,8 +88,8 @@ def main():
print_info()
Total = 0
fulltimes = '-{}{}{}{}{}'.format(dt.year,dt.month,dt.day,dt.hour,dt.minute) # 时间后缀
# times = fulltimes # 有时间后缀
times = '' # 无时间后缀
times = fulltimes # 有时间后缀
# times = '' # 无时间后缀
with open('data.csv') as f:
f_csv = csv.reader(f)
headers = next(f_csv)
@@ -82,7 +97,13 @@ def main():
with open('data{}.csv'.format(fulltimes), 'a+') as f0: # 写入检测后新data
# print('Channel,Group,Source,Link', file=f0)
for row in f_csv:
ret = check_channel(row,num)
try:
ret = check_channel(row,num)
except FunctionTimedOut as e:
#traceback.print_exc()
print('[{}] {}({}) Error:{}'.format(str(num), row[0], row[2], str(e)))
ret = False
if(ret): # 通过,写入
with open('groups/{}{}.txt'.format(row[1],times), 'a+') as f1:
print('{}({}-{}*{}),{}'.format(row[0],row[2],ret[0],ret[1],row[3]), file=f1)
@@ -95,6 +116,7 @@ def main():
print('{},{},{},{}'.format(row[0],row[1],row[2],row[3]), file=f0)
Total = Total + 1
num = num + 1
time.sleep(0.3)
print('Total: {}'.format(Total))
if __name__ == '__main__':
main()