Separate downloads fragments into their own temp dir

This commit is contained in:
ArabCoders 2023-12-27 16:28:42 +03:00
parent a26453448a
commit 46f1ea616c
2 changed files with 12 additions and 3 deletions

View file

@ -21,7 +21,6 @@ Your `yt-dlp` config should include the following options for optimal working co
```json ```json
{ {
"windowsfilenames": true, "windowsfilenames": true,
"continue_dl": true,
"live_from_start": true, "live_from_start": true,
"format_sort": [ "format_sort": [
"codec:avc:m4a" "codec:avc:m4a"

View file

@ -5,6 +5,7 @@ import logging
import multiprocessing import multiprocessing
import os import os
import re import re
import shutil
import yt_dlp import yt_dlp
from src.Utils import get_format, get_opts, jsonCookie, mergeConfig from src.Utils import get_format, get_opts, jsonCookie, mergeConfig
from src.DTO.ItemDTO import ItemDTO from src.DTO.ItemDTO import ItemDTO
@ -23,6 +24,7 @@ class Download:
info: ItemDTO = None info: ItemDTO = None
default_ytdl_opts: dict = None default_ytdl_opts: dict = None
debug: bool = False debug: bool = False
tempPath: str = None
_ytdlp_fields: tuple = ( _ytdlp_fields: tuple = (
'tmpfilename', 'tmpfilename',
@ -85,12 +87,17 @@ class Download:
} }
) )
# Create temp dir for each download.
self.tempPath = os.path.join(self.temp_dir, self.info._id)
if not os.path.exists(self.tempPath):
os.makedirs(self.tempPath, exist_ok=True)
params: dict = { params: dict = {
'no_color': True, 'no_color': True,
'format': self.format, 'format': self.format,
'paths': { 'paths': {
'home': self.download_dir, 'home': self.download_dir,
'temp': self.temp_dir 'temp': self.tempPath,
}, },
'outtmpl': { 'outtmpl': {
'default': self.output_template, 'default': self.output_template,
@ -111,7 +118,7 @@ class Download:
if not data: if not data:
logging.warning( logging.warning(
f'The cookie string that was provided for {self.info.title} is empty or not in expected spec.') f'The cookie string that was provided for {self.info.title} is empty or not in expected spec.')
with open(os.path.join(self.temp_dir, f'{self.info._id}.txt'), 'w') as f: with open(os.path.join(self.tempPath, f'cookie_{self.info._id}.txt'), 'w') as f:
f.write(data) f.write(data)
params['cookiefile'] = f.name params['cookiefile'] = f.name
@ -129,6 +136,9 @@ class Download:
except yt_dlp.utils.YoutubeDLError as exc: except yt_dlp.utils.YoutubeDLError as exc:
self.status_queue.put({'status': 'error', 'msg': str(exc)}) self.status_queue.put({'status': 'error', 'msg': str(exc)})
if self.tempPath and self.info._id and os.path.exists(self.tempPath):
shutil.rmtree(self.tempPath, ignore_errors=True)
async def start(self, notifier): async def start(self, notifier):
if self.manager is None: if self.manager is None:
self.manager = multiprocessing.Manager() self.manager = multiprocessing.Manager()