Skip to content

Commit

Permalink
Improved video download logic to not assume format is WebM, now uses …
Browse files Browse the repository at this point in the history
…YDL progress hook to get correct extension regardless
  • Loading branch information
beveradb committed Dec 21, 2023
1 parent 8924875 commit 90297bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ karaoke-prep "https://www.youtube.com/playlist?list=YOUR_PLAYLIST_ID"
After running `karaoke-prep` you should have the following 11 files, grouped into folder(s) for each track:
- `Artist - Title (YouTube xxxxxxxxxxx).webm`
- Original unmodified video, fetched from YouTube in the highest quality available
- Original unmodified video, fetched from YouTube in the highest quality available (may not always be WebM)
- You probably don't need this unless you're creating a custom request
- `Artist - Title (YouTube xxxxxxxxxxx).png`
- A still image taken from 30 seconds into the video, in case that's useful for a custom background
Expand Down
19 changes: 15 additions & 4 deletions karaoke_prep/karaoke_prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,28 @@ def get_youtube_id_for_top_search_result(self, query):

def download_video(self, youtube_id, output_filename_no_extension):
self.logger.debug(f"Downloading YouTube video {youtube_id} to filename {output_filename_no_extension} + (as yet) unknown extension")

downloaded_file_name = output_filename_no_extension
actual_file_extension = None

def ydl_progress_hook(d):
nonlocal actual_file_extension
if d["status"] == "finished":
actual_file_extension = d["filename"].split(".")[-1]

ydl_opts = {
"quiet": "True",
"quiet": True,
"format": "bv*+ba/b", # if a combined video + audio format is better than the best video-only format use the combined format
"outtmpl": f"{output_filename_no_extension}",
"progress_hooks": [ydl_progress_hook],
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
}

with yt_dlp.YoutubeDL(ydl_opts) as youtube_dl_instance:
youtube_dl_instance.download([f"https://www.youtube.com/watch?v={youtube_id}"])
self.logger.warn(f"Download finished, assuming hard-coded webm extension (!) and returning this filename")
# TODO: Replace hard-coded webm extension with the actual extension of the downloaded file using yt-dlp hooks / event callback
return output_filename_no_extension + ".webm"
downloaded_file_name += f".{actual_file_extension}"
self.logger.info(f"Download finished, returning downloaded filename: {downloaded_file_name}")
return downloaded_file_name

def extract_still_image_from_video(self, input_filename, output_filename_no_extension):
output_filename = output_filename_no_extension + ".png"
Expand Down Expand Up @@ -456,6 +466,7 @@ def prep_single_track(self):
processed_track["lyrics"] = lyrics_file
processed_track["processed_lyrics"] = processed_lyrics_file

# WebM may not always be the output format from YouTubeDL, but it's the most common and this is just a convenience cache
yt_webm_filename_pattern = os.path.join(track_output_dir, f"{artist_title} (YouTube *.webm")
yt_webm_glob = glob.glob(yt_webm_filename_pattern)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-prep"
version = "0.7.0"
version = "0.7.1"
description = "Prepare for karaoke video creation, by downloading audio and lyrics for a specified song or youtube playlist and separatung audio stems, then finalise the video with a title screen after manual syncing!"
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"
Expand Down

0 comments on commit 90297bd

Please sign in to comment.