Exemplo n.º 1
0
def create_file(input_file, link):
    log.info("Get metadata for {}".format(input_file))
    try:
        file_ext = '.{}'.format(input_file.split('.')[-1])
    except Exception as e:
        file_ext = ''

    meta_tags = spotify_tools.generate_metadata(link)
    if not meta_tags:
        log.error("No metadata found.")
    else:
        file_name = "{} - {}{}".format(meta_tags["artists"][0]["name"],
                                       meta_tags["name"], file_ext)
        log.info("Create {}".format(file_name))
        output_file = os.path.join(const.args.folder, file_name)
        command = (
            "ffmpeg -y -nostdin -hide_banner -nostats -v panic -i".split() +
            [input_file] + "-c copy -map_metadata -1 -map 0".split() +
            [output_file])

        try:
            subprocess.call(command)
            metadata.embed(output_file, meta_tags)
        except Exception as e:
            log.error(str(e))
Exemplo n.º 2
0
    def _download_single(self, songname):
        # deal with file formats containing slashes to non-existent directories
        songpath = os.path.join(const.args.folder, os.path.dirname(songname))
        os.makedirs(songpath, exist_ok=True)
        input_song = songname + const.args.input_ext
        output_song = songname + const.args.output_ext
        if youtube_tools.download_song(input_song, self.content):
            print("")
            try:
                convert.song(
                    input_song,
                    output_song,
                    const.args.folder,
                    avconv=const.args.avconv,
                    trim_silence=const.args.trim_silence,
                )
            except FileNotFoundError:
                encoder = "avconv" if const.args.avconv else "ffmpeg"
                log.warning(
                    "Could not find {0}, skip encoding".format(encoder))
                output_song = self.unconverted_filename(songname)

            if not const.args.no_metadata and self.meta_tags is not None:
                metadata.embed(os.path.join(const.args.folder, output_song),
                               self.meta_tags)
            return True
 def test_embed_in_flac(self):
     expect_embed = True
     embed = metadata.embed(track_path + ".flac", meta_tags)
     os.remove(track_path + ".flac")
     assert embed == expect_embed
 def test_embed_in_webm(self):
     expect_embed = False
     embed = metadata.embed(track_path + ".webm", meta_tags)
     os.remove(track_path + ".webm")
     assert embed == expect_embed
 def test_embed_in_mp3(self):
     expect_embed = True
     global track_path
     track_path = os.path.join(const.args.folder, file_name)
     embed = metadata.embed(track_path + ".mp3", meta_tags)
     assert embed == expect_embed
Exemplo n.º 6
0
def download_single(raw_song, number=None):
    """ Logic behind downloading a song. """

    if internals.is_youtube(raw_song):
        log.debug('Input song is a YouTube URL')
        content = youtube_tools.go_pafy(raw_song, meta_tags=None)
        raw_song = slugify(content.title).replace('-', ' ')
        meta_tags = spotify_tools.generate_metadata(raw_song)
    else:
        meta_tags = spotify_tools.generate_metadata(raw_song)
        content = youtube_tools.go_pafy(raw_song, meta_tags)

    if content is None:
        log.debug('Found no matching video')
        return

    if const.args.download_only_metadata and meta_tags is None:
        log.info('Found no metadata. Skipping the download')
        return

    # "[number]. [artist] - [song]" if downloading from list
    # otherwise "[artist] - [song]"
    youtube_title = youtube_tools.get_youtube_title(content, number)
    log.info('{} ({})'.format(youtube_title, content.watchv_url))

    # generate file name of the song to download
    songname = content.title

    if meta_tags is not None:
        refined_songname = internals.format_string(const.args.file_format,
                                                   meta_tags,
                                                   slugification=True)
        log.debug('Refining songname from "{0}" to "{1}"'.format(
            songname, refined_songname))
        if not refined_songname == ' - ':
            songname = refined_songname
    else:
        log.warning('Could not find metadata')
        songname = internals.sanitize_title(songname)

    if const.args.dry_run:
        return

    if not check_exists(songname, raw_song, meta_tags):
        # deal with file formats containing slashes to non-existent directories
        songpath = os.path.join(const.args.folder, os.path.dirname(songname))
        os.makedirs(songpath, exist_ok=True)
        input_song = songname + const.args.input_ext
        output_song = songname + const.args.output_ext
        if youtube_tools.download_song(input_song, content):
            try:
                convert.song(input_song,
                             output_song,
                             const.args.folder,
                             avconv=const.args.avconv,
                             trim_silence=const.args.trim_silence)
            except FileNotFoundError:
                encoder = 'avconv' if const.args.avconv else 'ffmpeg'
                log.warning(
                    'Could not find {0}, skipping conversion'.format(encoder))
                const.args.output_ext = const.args.input_ext
                output_song = songname + const.args.output_ext

            if not const.args.input_ext == const.args.output_ext:
                os.remove(os.path.join(const.args.folder, input_song))
            if not const.args.no_metadata and meta_tags is not None:
                metadata.embed(os.path.join(const.args.folder, output_song),
                               meta_tags)
            return True
Exemplo n.º 7
0
 def test_embed_in_flac(self, metadata_fixture, trackpath_fixture):
     expect_embed = True
     embed = metadata.embed(trackpath_fixture + ".flac", metadata_fixture)
     os.remove(trackpath_fixture + ".flac")
     assert embed == expect_embed
Exemplo n.º 8
0
 def test_embed_in_webm(self, metadata_fixture, trackpath_fixture):
     expect_embed = False
     embed = metadata.embed(trackpath_fixture + ".webm", metadata_fixture)
     os.remove(trackpath_fixture + ".webm")
     assert embed == expect_embed
Exemplo n.º 9
0
 def test_embed_in_mp3(self, metadata_fixture, trackpath_fixture):
     expect_embed = True
     embed = metadata.embed(trackpath_fixture + ".mp3", metadata_fixture)
     assert embed == expect_embed
Exemplo n.º 10
0
def download_single(raw_song, number=None):
    """ Logic behind downloading a song. """
    content, meta_tags = youtube_tools.match_video_and_metadata(raw_song)

    if content is None:
        log.debug("Found no matching video")
        return

    if const.args.download_only_metadata and meta_tags is None:
        log.info("Found no metadata. Skipping the download")
        return

    # "[number]. [artist] - [song]" if downloading from list
    # otherwise "[artist] - [song]"
    youtube_title = youtube_tools.get_youtube_title(content, number)
    log.info("{} ({})".format(youtube_title, content.watchv_url))

    # generate file name of the song to download
    songname = content.title

    if meta_tags is not None:
        refined_songname = internals.format_string(const.args.file_format,
                                                   meta_tags,
                                                   slugification=True)
        log.debug('Refining songname from "{0}" to "{1}"'.format(
            songname, refined_songname))
        if not refined_songname == " - ":
            songname = refined_songname
    else:
        if not const.args.no_metadata:
            log.warning("Could not find metadata")
        songname = internals.sanitize_title(songname)

    if const.args.dry_run:
        return

    if not check_exists(songname, raw_song, meta_tags):
        # deal with file formats containing slashes to non-existent directories
        songpath = os.path.join(const.args.folder, os.path.dirname(songname))
        os.makedirs(songpath, exist_ok=True)
        input_song = songname + const.args.input_ext
        output_song = songname + const.args.output_ext
        if youtube_tools.download_song(input_song, content):
            print("")
            try:
                convert.song(
                    input_song,
                    output_song,
                    const.args.folder,
                    avconv=const.args.avconv,
                    trim_silence=const.args.trim_silence,
                )
            except FileNotFoundError:
                encoder = "avconv" if const.args.avconv else "ffmpeg"
                log.warning(
                    "Could not find {0}, skipping conversion".format(encoder))
                const.args.output_ext = const.args.input_ext
                output_song = songname + const.args.output_ext

            if not const.args.input_ext == const.args.output_ext:
                os.remove(os.path.join(const.args.folder, input_song))
            if not const.args.no_metadata and meta_tags is not None:
                metadata.embed(os.path.join(const.args.folder, output_song),
                               meta_tags)
            return True
Exemplo n.º 11
0
 def test_embed_in_m4a(self):
     expect_embed = True
     embed = metadata.embed(track_path + '.m4a', meta_tags)
     os.remove(track_path + '.m4a')
     assert embed == expect_embed