Skip to content

Commit

Permalink
修复使用协程录制直播导致的中断问题
Browse files Browse the repository at this point in the history
  • Loading branch information
auqhjjqdo committed May 5, 2023
1 parent b1153da commit 64d3199
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ def get_streamlink(self, plugin_option: dict = None):
session.set_plugin_option(**plugin_option)
return session

async def run_record(self, stream: Union[StreamIO, HTTPStream], url, title, format):
def run_record(self, stream: Union[StreamIO, HTTPStream], url, title, format):
# 获取输出文件名
filename = self.get_filename(title, format)
if stream:
logger.info(f'{self.flag}开始录制:{filename}')
# 调用streamlink录制直播
await asyncio.to_thread(self.stream_writer, stream, url, filename) # 创建线程防止异步阻塞
self.stream_writer(stream, url, filename)
# format配置存在且不等于直播平台默认格式时运行ffmpeg封装
if self.format and self.format != format:
await asyncio.to_thread(self.run_ffmpeg, filename, format)
self.run_ffmpeg(filename, format)
recording.pop(url, None)
logger.info(f'{self.flag}停止录制:{filename}')
else:
Expand Down Expand Up @@ -180,7 +180,7 @@ async def run(self):
if response['data']['live_status'] == 1:
title = response['data']['title']
stream = self.get_streamlink().streams(url).get('best') # HTTPStream[flv]
await self.run_record(stream, url, title, 'flv')
await asyncio.to_thread(self.run_record, stream, url, title, 'flv')


class Douyu(LiveRecoder):
Expand All @@ -207,7 +207,7 @@ async def run(self):
self.get_streamlink(),
f'http://hdltc1.douyucdn.cn/live/{rtmp_id}_4000.flv'
) # HTTPStream[flv]
await self.run_record(stream, url, title, 'flv')
await asyncio.to_thread(self.run_record, stream, url, title, 'flv')


class Youtube(LiveRecoder):
Expand Down Expand Up @@ -241,7 +241,7 @@ async def run(self):
title = video['title']['runs'][0]['text']
if url not in recording:
stream = self.get_streamlink().streams(url).get('best') # HLSStream[mpegts]
asyncio.create_task(self.run_record(stream, url, title, 'ts'), name=url)
asyncio.create_task(asyncio.to_thread(self.run_record, stream, url, title, 'ts'))


class Twitch(LiveRecoder):
Expand Down Expand Up @@ -270,7 +270,7 @@ async def run(self):
'key': 'disable-ads',
'value': True,
}).streams(url).get('best') # HLSStream[mpegts]
await self.run_record(stream, url, title, 'ts')
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')


class Twitcasting(LiveRecoder):
Expand All @@ -292,7 +292,7 @@ async def run(self):
)).text
title = re.search('<meta name="twitter:title" content="(.*?)">', response).group(1)
stream = self.get_streamlink().streams(url).get('best') # Stream[mov,mp4,m4a,3gp,3g2,mj2]
await self.run_record(stream, url, title, 'mp4')
await asyncio.to_thread(self.run_record, stream, url, title, 'mp4')


async def run():
Expand Down

0 comments on commit 64d3199

Please sign in to comment.