Adding upload_year and upload_month to the video file format option.

This commit is contained in:
Roland 2025-12-23 18:49:33 -07:00
parent 9766f4b26f
commit 641fb368eb
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 92 additions and 3 deletions

View file

@ -254,6 +254,8 @@ function VideoFileNameForm({form}) {
<li><code>%(title)s</code> - Video title</li>
<li><code>%(uploader)s</code> - Channel/uploader name</li>
<li><code>%(upload_date)s</code> - Upload date (YYYYMMDD)</li>
<li><code>%(upload_year)s</code> - Upload year (YYYY)</li>
<li><code>%(upload_month)s</code> - Upload month (01-12)</li>
<li><code>%(id)s</code> - Video ID</li>
<li><code>%(ext)s</code> - File extension</li>
<li><code>%(channel)s</code> - Channel name</li>

View file

@ -115,6 +115,29 @@ def prepare_filename(entry: dict, ydl: YoutubeDL = YDL) -> str:
return f'{dir_name}/{file_name}'
def convert_wrolpi_filename_format(filename_format: str) -> str:
"""Convert WROLPi-specific filename variables to yt-dlp syntax.
WROLPi provides user-friendly variables that map to yt-dlp's strftime syntax:
%(upload_year)s -> %(upload_date>%Y)s
%(upload_month)s -> %(upload_date>%m)s
This allows users to organize videos into year/month subdirectories.
Example:
'%(upload_year)s/%(upload_month)s/%(title)s.%(ext)s'
-> '%(upload_date>%Y)s/%(upload_date>%m)s/%(title)s.%(ext)s'
"""
conversions = {
'%(upload_year)s': '%(upload_date>%Y)s',
'%(upload_month)s': '%(upload_date>%m)s',
}
result = filename_format
for wrolpi_var, ytdlp_var in conversions.items():
result = result.replace(wrolpi_var, ytdlp_var)
return result
def preview_filename(filename_format: str) -> str:
"""Return an example video file name formatted using the provided filename format.
@ -123,8 +146,11 @@ def preview_filename(filename_format: str) -> str:
if not filename_format.endswith('.%(ext)s'):
raise RuntimeError('Filename must end with .%(ext)s')
# Convert WROLPi-specific variables to yt-dlp syntax
converted_format = convert_wrolpi_filename_format(filename_format)
options = get_videos_downloader_config().yt_dlp_options
options['outtmpl'] = filename_format
options['outtmpl'] = converted_format
ydl = YoutubeDL(copy.deepcopy(options))
entry = dict(
uploader='WROLPi',
@ -135,7 +161,7 @@ def preview_filename(filename_format: str) -> str:
ext='mp4',
description='A description of the video',
)
filename = prepare_filename(entry, ydl=ydl)[1:]
filename = prepare_filename(entry, ydl=ydl).lstrip('/')
return filename
@ -663,8 +689,10 @@ class VideoDownloader(Downloader, ABC):
# YoutubeDL expects specific options, add onto the default options
config = get_videos_downloader_config()
options = config.yt_dlp_options
# Convert WROLPi-specific variables to yt-dlp syntax
converted_format = convert_wrolpi_filename_format(config.file_name_format)
# yt-dlp expects the absolute path.
options['outtmpl'] = f'{out_dir}/{config.file_name_format}'
options['outtmpl'] = f'{out_dir}/{converted_format}'
options['merge_output_format'] = video_format
# options['remuxvideo'] = video_format
options['format'] = video_resolutions

View file

@ -368,6 +368,20 @@ async def test_video_file_format(async_client, test_session, fake_now):
assert response.json['error'] == 'Filename must end with .%(ext)s'
@pytest.mark.asyncio
async def test_video_file_format_with_upload_year(async_client, test_session, fake_now):
"""Video file format API handles upload_year and upload_month variables."""
body = dict(video_file_format='%(upload_year)s/%(upload_month)s/%(title)s.%(ext)s')
request, response = await async_client.post('/api/videos/file_format', json=body)
assert response.status_code == HTTPStatus.OK
assert response.json['preview'] == '2000/01/The title of the video.mp4'
body = dict(video_file_format='%(upload_year)s/%(uploader)s_%(upload_date)s_%(title)s.%(ext)s')
request, response = await async_client.post('/api/videos/file_format', json=body)
assert response.status_code == HTTPStatus.OK
assert response.json['preview'] == '2000/WROLPi_20000101_The title of the video.mp4'
@pytest.mark.asyncio
async def test_video_upload_file_tracking(test_session, async_client, video_factory, await_switches,
make_multipart_form,

View file

@ -528,6 +528,51 @@ def test_preview_filename(test_directory, fake_now):
preview_filename('%(upload_date)s_%(title)s.asdf')
def test_convert_wrolpi_filename_format():
"""WROLPi filename variables are converted to yt-dlp syntax."""
from modules.videos.downloader import convert_wrolpi_filename_format
# Test upload_year conversion
assert convert_wrolpi_filename_format('%(upload_year)s/%(title)s.%(ext)s') \
== '%(upload_date>%Y)s/%(title)s.%(ext)s'
# Test upload_month conversion
assert convert_wrolpi_filename_format('%(upload_month)s/%(title)s.%(ext)s') \
== '%(upload_date>%m)s/%(title)s.%(ext)s'
# Test both together
assert convert_wrolpi_filename_format('%(upload_year)s/%(upload_month)s/%(title)s.%(ext)s') \
== '%(upload_date>%Y)s/%(upload_date>%m)s/%(title)s.%(ext)s'
# Test with other variables unchanged
format_str = '%(upload_year)s/%(uploader)s_%(upload_date)s_%(id)s_%(title)s.%(ext)s'
expected = '%(upload_date>%Y)s/%(uploader)s_%(upload_date)s_%(id)s_%(title)s.%(ext)s'
assert convert_wrolpi_filename_format(format_str) == expected
# Test format without WROLPi variables unchanged
original = '%(uploader)s_%(upload_date)s_%(id)s_%(title)s.%(ext)s'
assert convert_wrolpi_filename_format(original) == original
def test_preview_filename_with_upload_year(test_directory, fake_now):
"""preview_filename works with upload_year and upload_month variables."""
# Year only
assert preview_filename('%(upload_year)s/%(title)s.%(ext)s') \
== '2000/The title of the video.mp4'
# Month only
assert preview_filename('%(upload_month)s/%(title)s.%(ext)s') \
== '01/The title of the video.mp4'
# Year and month together
assert preview_filename('%(upload_year)s/%(upload_month)s/%(title)s.%(ext)s') \
== '2000/01/The title of the video.mp4'
# Combined with traditional format
assert preview_filename('%(upload_year)s/%(uploader)s_%(upload_date)s_%(title)s.%(ext)s') \
== '2000/WROLPi_20000101_The title of the video.mp4'
def test_normalize_video_file_names(test_directory, video_download_manager):
"""VideoDownloader renames caption files that do not share the video file's stem."""
downloader = VideoDownloader()