Getting file name from yt-dlp error when video file name is too long, trimming that long file name.

This commit is contained in:
Roland 2024-10-03 21:58:39 -06:00
parent 82e4e4797a
commit 584fdadcb7
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
8 changed files with 36 additions and 40 deletions

View file

@ -274,10 +274,6 @@ async def tag_channel(tag_name: str | None, directory: pathlib.Path | None, chan
channel = Channel.find_by_id(channel_id, session)
# Do not move Channel files into a directory with files.
if directory and directory != channel.directory and directory.is_dir() and next(directory.iterdir(), None):
raise ChannelDirectoryConflict('Channel directory already exists and is not empty')
# May also clear the tag if `tag_name` is None.
channel.set_tag(tag_name)
channel.flush()

View file

@ -584,6 +584,7 @@ async def test_move_channel_after_terminal_move(async_client, test_session, test
vid1 = video_factory(channel_id=channel.id)
test_session.commit()
# TODO this does not seem to work
assert str(channel.directory) == str(test_directory / 'videos/Channel Name')
assert str(vid1.video_path).startswith(str(test_directory / 'videos/Channel Name/'))

View file

@ -485,7 +485,7 @@ class VideoDownloader(Downloader, ABC):
@staticmethod
def prepare_filename(url: str, out_dir: pathlib.Path) -> Tuple[pathlib.Path, dict]:
"""Get the full path of a video file from its URL."""
"""Get the full path of a video file from its URL using yt-dlp."""
if not out_dir.is_dir():
raise ValueError(f'Output directory does not exist! {out_dir=}')
@ -506,28 +506,35 @@ class VideoDownloader(Downloader, ABC):
try:
entry = extract_info(url, ydl=ydl, process=True)
final_filename = pathlib.Path(prepare_filename(entry, ydl=ydl)).absolute()
final_filename = trim_file_name(final_filename)
except DownloadError as e:
if ' Cannot write ' in str(e):
# yt-dlp does not handle long file names well, get the name from the error (lol)
last_line = str(e).splitlines()[-1]
filename = last_line.split(' file ')[-1].strip()
full_path, _ = split_path_stem_and_suffix(filename, full=True)
if not full_path.startswith('/'):
logger.error(f'Failed to extract filename from {last_line}')
raise
# Trim long filename, add video suffix.
final_filename = pathlib.Path(f'{full_path}.{PREFERRED_VIDEO_EXTENSION}')
final_filename = trim_file_name(final_filename)
# Get entry info json.
options['outtmpl'] = str(final_filename)
ydl = YoutubeDL(options)
ydl.params['logger'] = ydl_logger
ydl.add_default_info_extractors()
entry = extract_info(url, ydl=ydl, process=True)
else:
if ' Cannot write ' not in str(e):
raise
# yt-dlp does not handle long file names well, get the name from the error (lol)
last_line = str(e).splitlines()[-1]
full_path = last_line.split(' file ')[-1].strip()
if not full_path.startswith('/'):
logger.error(f'Failed to extract filename from {last_line}')
raise
# Split filename from parent.
full_path = pathlib.Path(full_path)
parent = full_path.parent
filename, _ = split_path_stem_and_suffix(full_path.name)
# Trim long filename, add video suffix, add back into parent directory.
filename = escape_file_name(filename)
final_filename = parent / f'{filename}.{PREFERRED_VIDEO_EXTENSION}'
final_filename = trim_file_name(final_filename)
logger.debug(f'Video file name was too long. Trimmed to: {final_filename.name}')
# Get entry info json.
options['outtmpl'] = str(final_filename)
ydl = YoutubeDL(options)
ydl.params['logger'] = ydl_logger
ydl.add_default_info_extractors()
entry = extract_info(url, ydl=ydl, process=True)
logger.debug(f'Downloading {url} to {repr(str(final_filename))}')
return final_filename, entry

View file

@ -311,7 +311,6 @@ class ChannelsConfig(ConfigFile):
link_channel_and_downloads(session)
self.successful_import = True
channel_import_logger.info('Importing channels config complete')
except Exception as e:
self.successful_import = False
message = f'Failed to import {self.get_relative_file()} config!'