Skip to content

Commit

Permalink
Improve extracting metadata from youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
igrek51 committed Jun 17, 2020
1 parent 08e7ae5 commit 4f82e4a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
24 changes: 14 additions & 10 deletions trimmer/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def download_from_youtube(url: str) -> str:
return full_filename


def fetch_youtube_title(url: str) -> str:
def fetch_youtube_metadata(url: str) -> Tuple[str, str, str]:
with wrap_context('fetching title from youtube', url=url):
info('fetching title from youtube...', url=url)
info('fetching metadata from youtube page...', url=url)

ydl_opts = {
'format': 'bestaudio/best',
Expand All @@ -51,14 +51,18 @@ def fetch_youtube_title(url: str) -> str:
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
einfo = ydl.extract_info(url, download=False)
filename = ydl.prepare_filename(einfo)
title = filename[:-4]
info('youtube title fetched', yt_title=title)
return title

track = einfo.get('track')
artist = einfo.get('artist') or einfo.get('creator')
full_title = einfo.get('title') or einfo.get('alt_title')

info('youtube page metadata fetched', yt_title=full_title, artist=artist, track=track)
return artist, track, full_title


def extract_youtube_artist_title(url: str) -> Tuple[str, str]:
yt_title = fetch_youtube_title(url)
artist, title = extract_artist_title(yt_title)
info('artist & title extracted from youtube page', artist=artist, title=title)
return artist, title
artist, track, full_title = fetch_youtube_metadata(url)
if artist and track:
return artist, track

return extract_artist_title(full_title)
6 changes: 4 additions & 2 deletions trimmer/normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def normalize_song(mp3_file: str, no_trim: bool, no_fade: bool, no_normalize: bo
song = AudioSegment.from_mp3(mp3_file)

if not no_normalize:
info('normalizing volume level...')
gain = -song.max_dBFS
max_dbfs = song.max_dBFS
loudness = song.dBFS
info('normalizing volume level...', max_dBFS=f'{max_dbfs:.2f}dB', dBFS=f'{loudness:.2f}dB')
gain = -max_dbfs
if user_gain is not None:
gain = user_gain
song = song.apply_gain(gain)
Expand Down
13 changes: 7 additions & 6 deletions trimmer/trim_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ def trim_from_source(source: str, artist: Optional[str], title: Optional[str],
raise RuntimeError(f'unrecognized source: {source}')


def trim_url(url: str, artist: Optional[str], title: Optional[str],
def trim_url(url: str, user_artist: Optional[str], user_title: Optional[str],
no_trim: bool, no_fade: bool, no_normalize: bool,
trim_start: Optional[float] = None, trim_end: Optional[float] = None, gain: Optional[float] = None):
with wrap_context('url song'):
yt_artist, yt_title = extract_youtube_artist_title(url)
artist = artist or enter_or_default('Artist', default=yt_artist)
title = title or enter_or_default('Title', default=yt_title)
info('artist & title extracted from youtube page', artist=yt_artist, title=yt_title)
artist = user_artist or enter_or_default('Artist', default=yt_artist)
title = user_title or enter_or_default('Title', default=yt_title)

mp3_file = download_from_youtube(url)
mp3_file = rename_song(mp3_file, artist, title)
Expand All @@ -40,15 +41,15 @@ def trim_url(url: str, artist: Optional[str], title: Optional[str],
info('song saved', mp3_file=mp3_file)


def trim_mp3(file: str, artist: Optional[str], title: Optional[str],
def trim_mp3(file: str, user_artist: Optional[str], user_title: Optional[str],
no_trim: bool, no_fade: bool, no_normalize: bool,
trim_start: Optional[float] = None, trim_end: Optional[float] = None, gain: Optional[float] = None):
with wrap_context('mp3 song'):
assert os.path.isfile(file), 'input file should exist'

tag_artist, tag_title = read_mp3_artist_title(file)
artist = artist or tag_artist or input('Artist: ')
title = title or tag_title or input('Title: ')
artist = user_artist or tag_artist or enter_or_default('Artist')
title = user_title or tag_title or enter_or_default('Title')

mp3_file = rename_song(file, artist, title)
normalize_song(mp3_file, no_trim, no_fade, no_normalize, trim_start, trim_end, gain)
Expand Down
2 changes: 1 addition & 1 deletion trimmer/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.8"
__version__ = "0.1.9"

0 comments on commit 4f82e4a

Please sign in to comment.