Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.21
1.8.22
36 changes: 18 additions & 18 deletions src/torchlight/FFmpegAudioPlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ def __del__(self) -> None:
self.logger.debug("~FFmpegAudioPlayer()")
self.Stop()

async def _write_to_ffmpeg(self, chunk: bytes) -> None:
proc = self.ffmpeg_process
if proc and proc.stdin and not proc.stdin.is_closing():
proc.stdin.write(chunk)
await proc.stdin.drain()

async def _close_ffmpeg_stdin(self) -> None:
if self.ffmpeg_process and self.ffmpeg_process.stdin:
try:
self.ffmpeg_process.stdin.close()
await self.ffmpeg_process.stdin.wait_closed()
except Exception as e:
self.logger.debug("Failed to cleanly close FFmpeg stdin: %s", e)

async def _stream_url_to_ffmpeg(self, uri: str, ffmpeg_command: list[str], needs_cf_bypass: bool = False) -> None:
parsed = urlparse(uri)
is_local_file = parsed.scheme in ("file", "") or os.path.exists(uri)
Expand Down Expand Up @@ -189,17 +203,10 @@ def sync_fetch_curl_cffi() -> None:
if chunk is None:
break

if proc.stdin and not proc.stdin.is_closing():
proc.stdin.write(chunk)
await proc.stdin.drain()
await self._write_to_ffmpeg(chunk)
finally:
await fetch_future
if self.ffmpeg_process and self.ffmpeg_process.stdin:
try:
self.ffmpeg_process.stdin.close()
await self.ffmpeg_process.stdin.wait_closed()
except Exception as e:
self.logger.debug("Failed to cleanly close FFmpeg stdin: %s", e)
await self._close_ffmpeg_stdin()
return

timeout = aiohttp.ClientTimeout(total=None, connect=10.0, sock_read=15.0)
Expand Down Expand Up @@ -244,9 +251,7 @@ def sync_fetch_curl_cffi() -> None:

bytes_downloaded += len(chunk)

if self.ffmpeg_process.stdin:
self.ffmpeg_process.stdin.write(chunk)
await self.ffmpeg_process.stdin.drain()
await self._write_to_ffmpeg(chunk)

break

Expand All @@ -262,12 +267,7 @@ def sync_fetch_curl_cffi() -> None:
except Exception as e:
self.logger.error("Unexpected streaming error: %s", e, exc_info=True)
finally:
if self.ffmpeg_process and self.ffmpeg_process.stdin:
try:
self.ffmpeg_process.stdin.close()
await self.ffmpeg_process.stdin.wait_closed()
except Exception as e:
self.logger.debug("Failed to cleanly close FFmpeg stdin: %s", e)
await self._close_ffmpeg_stdin()
if self.session and not self.session.closed:
await self.session.close()
self.session = None
Expand Down
Loading