Commit 8ec0a44e authored by Javinator9889's avatar Javinator9889 🎼

Audio conversion preserving metadata

parent 49bc1faf
Pipeline #80 failed with stage
in 75 minutes and 8 seconds
......@@ -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
......@@ -47,3 +47,51 @@ class FFmpegOpener(object):
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()
......@@ -35,13 +35,6 @@ class YouTubeDownloader(object):
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: " +
......
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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment