Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Audio conversion preserving metadata
  • Loading branch information
Javinator9889 committed Oct 8, 2019
1 parent 49bc1fa commit 8ec0a44
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
2 changes: 2 additions & 0 deletions YouTubeMDBot/audio/__init__.py
Expand Up @@ -14,5 +14,7 @@
# 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 ..audio.ffmpeg import FFmpegOpener
from ..audio.ffmpeg import FFmpegOGG
from ..audio.ffmpeg import FFmpegMP3
from ..audio.ffmpeg import ffmpeg_available
from ..audio.fpcalc import FPCalc
48 changes: 48 additions & 0 deletions YouTubeMDBot/audio/ffmpeg.py
Expand Up @@ -47,3 +47,51 @@ def get_output(self) -> bytes:

def get_extra(self) -> bytes:
return self.__err


class FFmpegExporter:
def __init__(self, data: BytesIO):
self._data = data
self.__command = ["ffmpeg", "-i", "-", "-vn", "-map_metadata", "0",
"-movflags", "use_metadata_tags"]
self.__out = None
self.__err = None

def _call_ffmpeg(self):
self._data.seek(0)
proc = Popen(self.__command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
self.__out, self.__err = proc.communicate(self._data.read())

def _get_command(self) -> list:
return self.__command

def convert(self):
raise NotImplementedError

def get_output(self) -> bytes:
return self.__out

def get_err(self) -> bytes:
return self.__err


class FFmpegMP3(FFmpegExporter):
def convert(self):
command = super()._get_command()
command.append("-acodec")
command.append("libmp3lame")
command.append("-f")
command.append("mp3")
command.append("-")
self._call_ffmpeg()


class FFmpegOGG(FFmpegExporter):
def convert(self):
command = super()._get_command()
command.append("-c:a")
command.append("libvorbis")
command.append("-f")
command.append("ogg")
command.append("-")
self._call_ffmpeg()
7 changes: 0 additions & 7 deletions YouTubeMDBot/downloader/youtube_downloader.py
Expand Up @@ -35,13 +35,6 @@ def download(self, ffmpeg: bool = False) -> Tuple[BytesIO, bytes]:
stdout, stderr = proc.communicate()
retcode = proc.returncode
if retcode == 0:
# if ffmpeg:
# opener = FFmpegOpener(stdout)
# opener.open()
# stdout = opener.get_output()
# err = opener.get_extra()
# if err:
# print(err.decode("utf-8"))
return BytesIO(stdout), stdout
else:
raise RuntimeError("youtube-dl downloader exception - more info: " +
Expand Down
37 changes: 37 additions & 0 deletions YouTubeMDBot/tests/converter.py
@@ -0,0 +1,37 @@
import unittest
import mutagen

from typing import Tuple
from io import BytesIO

from YouTubeMDBot.tests.tagger import TaggerTest
from YouTubeMDBot.downloader import YouTubeDownloader
from YouTubeMDBot.audio import FFmpegMP3
from YouTubeMDBot.audio import FFmpegOGG


class MyTestCase(TaggerTest):
def find_metadata(self, downloader: YouTubeDownloader) -> Tuple[BytesIO, bytes]:
io, data = super().find_metadata(downloader)
io.seek(0)
mp3 = FFmpegMP3(data=io)
ogg = FFmpegOGG(data=io)

mp3.convert()
io.seek(0)
ogg.convert()

mp3_container = BytesIO(mp3.get_output())
ogg_container = BytesIO(ogg.get_output())

print(mp3.get_err().decode("utf-8"))
print(ogg.get_err().decode("utf-8"))

print(mutagen.File(mp3_container).pprint())
print(mutagen.File(ogg_container).pprint())

return io, data


if __name__ == '__main__':
unittest.main()

0 comments on commit 8ec0a44

Please sign in to comment.