Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Metadata for downloaded audio - milestone #3
- Loading branch information
1 parent
08aaff2
commit a4da41a
Showing
20 changed files
with
174 additions
and
23 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# YouTubeMDBot | ||
# Copyright (C) 2019 - Javinator9889 | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
from mutagen.mp4 import MP4 | ||
from mutagen.mp4 import MP4Cover | ||
from io import BytesIO | ||
|
||
|
||
class AudioMetadata: | ||
def __init__(self, audio: BytesIO): | ||
self._audio = MP4(audio) | ||
self._data = audio | ||
|
||
def set_title(self, title: str): | ||
self._audio[u"\xa9nam"] = title | ||
|
||
def set_artist(self, artist: str): | ||
self._audio[u"\xa9ART"] = artist | ||
|
||
def set_album(self, album: str): | ||
self._audio[u"\xa9alb"] = album | ||
|
||
def set_extras(self, extras: list): | ||
self._audio[u"\xa9cmt"] = '; '.join(map(str, extras)) | ||
|
||
def set_cover(self, cover: bytes): | ||
mp4_cover = MP4Cover(cover, MP4Cover.FORMAT_JPEG) | ||
self._audio[u"covr"] = [mp4_cover] | ||
|
||
def save(self) -> BytesIO: | ||
self._data.seek(0) | ||
self._audio.save(self._data) | ||
return self._data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import unittest | ||
import mutagen | ||
|
||
from typing import Tuple | ||
from io import BytesIO | ||
|
||
from YouTubeMDBot.tests.identifier import IdentifierTest | ||
from YouTubeMDBot.downloader import YouTubeDownloader | ||
from YouTubeMDBot.metadata import AudioMetadata | ||
from YouTubeMDBot.utils import youtube_utils | ||
|
||
|
||
class TaggerTest(IdentifierTest): | ||
def find_metadata(self, downloader: YouTubeDownloader) -> Tuple[BytesIO, bytes]: | ||
io, data = super().find_metadata(downloader) | ||
tagger = AudioMetadata(io) | ||
url = downloader.get_url() | ||
|
||
tagger.set_title(super().song_info[url]["title"]) | ||
tagger.set_artist(super().song_info[url]["artist"]) | ||
tagger.set_cover(super().song_info[url]["cover"]) | ||
extra = ["YouTube URL: " + url] | ||
if not super().song_info[url].get("youtube_data"): | ||
tagger.set_album(super().song_info[url]["album"]) | ||
extra.append("MusicBrainz Record ID: " + super().song_info[url][ | ||
"record_id"]) | ||
extra.append("MusicBrainz Release ID: " + super().song_info[url][ | ||
"release_id"]) | ||
tagger.set_extras(extra) | ||
else: | ||
tagger.set_extras(["YouTube ID: {}".format(super().song_info[url]["id"])]) | ||
yid = youtube_utils.get_yt_video_id(url) | ||
rs = tagger.save() | ||
rs.seek(0) | ||
print(mutagen.File(rs).pprint()) | ||
rs.seek(0) | ||
with open(yid + ".m4a", "wb") as f: | ||
f.write(rs.read()) | ||
rs.seek(0) | ||
return rs, rs.read() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# YouTubeMDBot | ||
# Copyright (C) 2019 - Javinator9889 | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
import signal | ||
|
||
|
||
class timeout: | ||
def __init__(self, secs: int): | ||
self.__secs = secs | ||
|
||
def _raise_timeout(self, signum, frame): | ||
raise TimeoutError("Function timeout! - {0}s".format(self.__secs)) | ||
|
||
def __enter__(self): | ||
if self.__secs <= 0: | ||
self._raise_timeout(0, 0) | ||
|
||
signal.signal(signal.SIGALRM, self._raise_timeout) | ||
signal.alarm(self.__secs) | ||
yield | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
signal.signal(signal.SIGALRM, signal.SIG_IGN) | ||
return exc_val is not None | ||
|
||
|
||
'''@contextmanager | ||
def timeout(secs: int): | ||
def raise_timeout(signum=None, frame=None): | ||
raise TimeoutError("Function timeout! - {0}s".format(secs)) | ||
if secs <= 0: | ||
secs = 0 | ||
raise_timeout() | ||
signal.signal(signalnum=signal.SIGALRM, handler=raise_timeout) | ||
signal.alarm(secs) | ||
try: | ||
yield | ||
except TimeoutError: | ||
pass | ||
finally: | ||
signal.signal(signalnum=signal.SIGALRM, handler=signal.SIG_IGN)''' |