11import os
22import PySimpleGUI as sg
33import subprocess as sp
4+ import youtube_dl
5+ import yaml
46
57
68def set_proxy (in_port ):
79 port = in_port
810 os .environ ["http_proxy" ] = f"http://127.0.0.1:{ port } "
911 os .environ ["https_proxy" ] = f"http://127.0.0.1:{ port } "
12+ sg .cprint ('设置代理成功' )
1013
1114
12- def get_m3u8 (in_youtube , in_url ):
13- youtube_path = in_youtube
15+ def get_format (in_url ):
1416 url = in_url
15- m3u8 = os .popen (f'{ youtube_path } -g { url } ' ).read ().strip ()
16- sg .cprint (f'获取m3u8成功' )
17- return m3u8
17+ ydl = youtube_dl .YoutubeDL ({'outtmpl' : '%(id)s.%(ext)s' })
18+ try :
19+ with ydl :
20+ result = ydl .extract_info (f'{ url } ' , download = False )
21+ if result ['is_live' ]:
22+ sg .cprint ('获取直播信息成功' )
23+ return result ['formats' ]
24+ else :
25+ sg .cprint ('获取直播信息失败,直播可能已经停止' )
26+ except :
27+ sg .cprint ('检查直播链接输入是否正确' )
28+
1829
1930g_process = None
31+
32+
2033def restream (m3u8 , in_ffmpeg , in_key ):
2134 global g_process
2235 ffmpeg_path = in_ffmpeg
2336 key = in_key
24- g_process = sp .Popen (
25- [ffmpeg_path , '-i' , m3u8 , '-vcodec' , 'libx264' , '-acodec' , 'aac' , '-f' , 'flv' , f"rtmp://6721.livepush.myqcloud.com/live/{ key } " ], stdout = sp .PIPE , stderr = sp .STDOUT , universal_newlines = True )
26- for line in g_process .stdout :
27- if 'speed' in line :
28- sg .cprint (line .rstrip ("\n " ))
37+ try :
38+ g_process = sp .Popen (
39+ [ffmpeg_path , '-i' , m3u8 , '-vcodec' , 'copy' , '-acodec' , 'aac' , '-f' , 'flv' , f"rtmp://6721.livepush.myqcloud.com/live/{ key } " ], stdout = sp .PIPE , stderr = sp .STDOUT , universal_newlines = True )
40+ for line in g_process .stdout :
41+ if 'speed' in line :
42+ sg .cprint (line .rstrip ("\n " ))
43+ except :
44+ sg .cprint ('推流失败' )
2945
30- def stop_restream ():
46+
47+ def stop_restream ():
3148 global g_process
3249 if g_process is not None :
33- g_process .kill ()
50+ g_process .kill ()
51+
52+
53+ def write_yaml (r ):
54+
55+ with open ('config.yaml' , "w" , encoding = "utf-8" ) as f :
56+ yaml .dump (r , f )
57+
58+
59+ def read_yaml (file ):
60+ try :
61+ with open (file , 'r' ) as f :
62+ yaml_dic = yaml .load (f , Loader = yaml .FullLoader )
63+ return yaml_dic
64+ except :
65+ sg .Popup ('未发现保存的配置' )
0 commit comments