convert: playlist: Generate m3u file in one batch

to avoid any possible interference with the threaded file conversion
mechanism.
This commit is contained in:
J0J0 T 2022-07-10 15:18:42 +02:00 committed by J0J0 Todos
parent d589e77ade
commit c251ed19c4

View file

@ -267,7 +267,7 @@ class ConvertPlugin(BeetsPlugin):
util.displayable_path(source)) util.displayable_path(source))
def convert_item(self, dest_dir, keep_new, path_formats, fmt, def convert_item(self, dest_dir, keep_new, path_formats, fmt,
pretend=False, link=False, hardlink=False, playlist=None): pretend=False, link=False, hardlink=False):
"""A pipeline thread that converts `Item` objects from a """A pipeline thread that converts `Item` objects from a
library. library.
""" """
@ -292,26 +292,6 @@ class ConvertPlugin(BeetsPlugin):
dest = replace_ext(dest, ext) dest = replace_ext(dest, ext)
converted = dest converted = dest
# When the playlist argument is passed, add the current filename to
# an m3u8 playlist file located in the destination folder.
if playlist:
self._log.debug(
"Appending to playlist file {0}",
util.displayable_path(playlist)
)
# The classic m3u format doesn't support special characters in
# media file paths, thus we use the m3u8 format which requires
# media file paths to be unicode. Additionally we use relative
# paths to ensure readability of the playlist on remote
# computers.
dest_relative = item.destination(
basedir=dest_dir,
path_formats=path_formats,
fragment=True
)
with open(playlist, "a") as playlist_file:
playlist_file.write(dest_relative + "\n")
# Ensure that only one thread tries to create directories at a # Ensure that only one thread tries to create directories at a
# time. (The existence check is not atomic with the directory # time. (The existence check is not atomic with the directory
# creation inside this function.) # creation inside this function.)
@ -492,13 +472,26 @@ class ConvertPlugin(BeetsPlugin):
link, hardlink) link, hardlink)
if playlist: if playlist:
# When playlist arg is passed create an m3u8 file in dest folder.
#
# The classic m3u format doesn't support special characters in
# media file paths, thus we use the m3u8 format which requires
# media file paths to be unicode. Additionally we use relative
# paths to ensure readability of the playlist on remote
# computers.
self._log.info("Creating playlist file: {0}", playlist) self._log.info("Creating playlist file: {0}", playlist)
items_paths = [
item.destination(
basedir=dest, path_formats=path_formats, fragment=True
) for item in items
]
items_paths = ["#EXTM3U"] + items_paths
if not pretend: if not pretend:
with open(playlist, "w") as playlist_file: with open(playlist, "w") as playlist_file:
playlist_file.write("#EXTM3U" + "\n") playlist_file.writelines('\n'.join(items_paths))
self._parallel_convert(dest, opts.keep_new, path_formats, fmt, pretend, self._parallel_convert(dest, opts.keep_new, path_formats, fmt, pretend,
link, hardlink, threads, items, playlist) link, hardlink, threads, items)
def convert_on_import(self, lib, item): def convert_on_import(self, lib, item):
"""Transcode a file automatically after it is imported into the """Transcode a file automatically after it is imported into the
@ -603,7 +596,7 @@ class ConvertPlugin(BeetsPlugin):
playlist) playlist)
def _parallel_convert(self, dest, keep_new, path_formats, fmt, def _parallel_convert(self, dest, keep_new, path_formats, fmt,
pretend, link, hardlink, threads, items, playlist): pretend, link, hardlink, threads, items):
"""Run the convert_item function for every items on as many thread as """Run the convert_item function for every items on as many thread as
defined in threads defined in threads
""" """
@ -613,8 +606,7 @@ class ConvertPlugin(BeetsPlugin):
fmt, fmt,
pretend, pretend,
link, link,
hardlink, hardlink)
playlist)
for _ in range(threads)] for _ in range(threads)]
pipe = util.pipeline.Pipeline([iter(items), convert]) pipe = util.pipeline.Pipeline([iter(items), convert])
pipe.run_parallel() pipe.run_parallel()