示例#1
0
def _get_mp3(path):
    try:
        mp3 = mutagen.mp3.MP3(path)
    # TODO(trow): We might need to catch other exceptions too.
    except (mutagen.mp3.HeaderNotFoundError,
            # The frame-parsing code in mutagen/mp3.py does not
            # do strict-enough error checking, and can raise
            # struct.error when trying to parse a malformed file.
            struct.error):
        return None
    # Automatically clean up the text tags.
    for tag in mp3.itervalues():
        id3_text.standardize(tag)
    return mp3
示例#2
0
def _get_mp3(path):
    try:
        mp3 = mutagen.mp3.MP3(path)
    # TODO(trow): We might need to catch other exceptions too.
    except (
            mutagen.mp3.HeaderNotFoundError,
            # The frame-parsing code in mutagen/mp3.py does not
            # do strict-enough error checking, and can raise
            # struct.error when trying to parse a malformed file.
            struct.error):
        return None
    # Automatically clean up the text tags.
    for tag in mp3.itervalues():
        id3_text.standardize(tag)
    return mp3
示例#3
0
    def test_scan_fast_tag_handling(self):
        test_mp3 = mutagen.mp3.MP3()

        class MockInfo(object):
            pass

        test_mp3.info = MockInfo()
        test_mp3.add_tags()
        test_mp3.tags.add(ufid.ufid_tag(TEST_VOL, TEST_TS, TEST_FP))
        test_mp3.tags.add(mutagen.id3.TLEN(text=u"11111"))
        test_mp3.tags.add(
            mutagen.id3.TXXX(desc=constants.TXXX_ALBUM_ID_DESCRIPTION,
                             text=[u"222"]))
        test_mp3.tags.add(
            mutagen.id3.TXXX(desc=constants.TXXX_FRAME_COUNT_DESCRIPTION,
                             text=[u"333"]))
        test_mp3.tags.add(
            mutagen.id3.TXXX(desc=constants.TXXX_FRAME_SIZE_DESCRIPTION,
                             text=["444"]))
        for tag in test_mp3.tags.values():
            id3_text.standardize(tag)
        test_mp3.info.sample_rate = 5555
        test_mp3.info.bitrate = 6666
        test_mp3.info.mode = 2

        au_file = audio_file.scan_fast("/test/path",
                                       _read_id3_hook=lambda p: test_mp3)
        self.assertTrue(audio_file is not None)
        self.assertEqual("/test/path", au_file.path)
        self.assertEqual(TEST_VOL, au_file.volume)
        self.assertEqual(TEST_TS, au_file.import_timestamp)
        self.assertEqual(TEST_FP, au_file.fingerprint)
        self.assertEqual(11111, au_file.duration_ms)
        self.assertEqual(222, au_file.album_id)
        self.assertEqual(333, au_file.frame_count)
        self.assertEqual(444, au_file.frame_size)
        self.assertEqual(5555, au_file.mp3_header.sampling_rate_hz)
        self.assertEqual(6.666, au_file.mp3_header.bit_rate_kbps)
        self.assertEqual(2, au_file.mp3_header.channels)
    def test_scan_fast_tag_handling(self):
        test_mp3 = mutagen.mp3.MP3()
        class MockInfo(object): pass
        test_mp3.info = MockInfo()
        test_mp3.add_tags()
        test_mp3.tags.add(ufid.ufid_tag(TEST_VOL, TEST_TS, TEST_FP))
        test_mp3.tags.add(mutagen.id3.TLEN(text=u"11111"))
        test_mp3.tags.add(mutagen.id3.TXXX(
                desc=constants.TXXX_ALBUM_ID_DESCRIPTION,
                text=[u"222"]))
        test_mp3.tags.add(mutagen.id3.TXXX(
                desc=constants.TXXX_FRAME_COUNT_DESCRIPTION,
                text=[u"333"]))
        test_mp3.tags.add(mutagen.id3.TXXX(
                desc=constants.TXXX_FRAME_SIZE_DESCRIPTION,
                text=["444"]))
        for tag in test_mp3.tags.values():
            id3_text.standardize(tag)
        test_mp3.info.sample_rate = 5555
        test_mp3.info.bitrate = 6666
        test_mp3.info.mode = 2

        au_file = audio_file.scan_fast("/test/path",
                                       _read_id3_hook=lambda p: test_mp3)
        self.assertTrue(audio_file is not None)
        self.assertEqual("/test/path", au_file.path)
        self.assertEqual(TEST_VOL, au_file.volume)
        self.assertEqual(TEST_TS, au_file.import_timestamp)
        self.assertEqual(TEST_FP, au_file.fingerprint)
        self.assertEqual(11111, au_file.duration_ms)
        self.assertEqual(222, au_file.album_id)
        self.assertEqual(333, au_file.frame_count)
        self.assertEqual(444, au_file.frame_size)
        self.assertEqual(5555, au_file.mp3_header.sampling_rate_hz)
        self.assertEqual(6.666, au_file.mp3_header.bit_rate_kbps)
        self.assertEqual(2, au_file.mp3_header.channels)
    def test_standardize(self):
        # Check basic functionality against a vanilla tag.
        tag = mutagen.id3.TPE1(encoding=1,
                               text=["Bad whitespace   ",
                                     " ",  # Empty should be stripped out
                                     "  Bad    whitespace"])
        id3_text.standardize(tag)
        self.assertEqual(constants.DEFAULT_ID3_TEXT_ENCODING, tag.encoding)
        self.assertEqual(["Bad whitespace", "Bad whitespace"], tag.text)
        # Passing in a non-text tag should be harmless.
        id3_text.standardize(mutagen.id3.UFID())

        # Check that we fix the encoding on non-vanilla tags.
        tag = mutagen.id3.TDRC(encoding=1, year=2009)
        id3_text.standardize(tag)
        self.assertEqual(constants.DEFAULT_ID3_TEXT_ENCODING, tag.encoding)
示例#6
0
    def test_standardize(self):
        # Check basic functionality against a vanilla tag.
        tag = mutagen.id3.TPE1(
            encoding=1,
            text=[
                "Bad whitespace   ",
                " ",  # Empty should be stripped out
                "  Bad    whitespace"
            ])
        id3_text.standardize(tag)
        self.assertEqual(constants.DEFAULT_ID3_TEXT_ENCODING, tag.encoding)
        self.assertEqual(["Bad whitespace", "Bad whitespace"], tag.text)
        # Passing in a non-text tag should be harmless.
        id3_text.standardize(mutagen.id3.UFID())

        # Check that we fix the encoding on non-vanilla tags.
        tag = mutagen.id3.TDRC(encoding=1, year=2009)
        id3_text.standardize(tag)
        self.assertEqual(constants.DEFAULT_ID3_TEXT_ENCODING, tag.encoding)