Exemplo n.º 1
0
    def test_padding(self):
        AIFF(self.filename_1).save()
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1002)
        AIFF(self.filename_1).save()
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1002)

        tags = AIFF(self.filename_1)
        tags.save(padding=lambda x: 1)
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1)

        tags = AIFF(self.filename_1)
        tags.save(padding=lambda x: 100)
        self.assertEqual(AIFF(self.filename_1).tags._padding, 100)

        tags = AIFF(self.filename_1)
        self.assertRaises(AIFFError, tags.save, padding=lambda x: -1)
Exemplo n.º 2
0
    def test_padding(self):
        AIFF(self.filename_1).save()
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1002)
        AIFF(self.filename_1).save()
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1002)

        tags = AIFF(self.filename_1)
        tags.save(padding=lambda x: 1)
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1)

        tags = AIFF(self.filename_1)
        tags.save(padding=lambda x: 100)
        self.assertEqual(AIFF(self.filename_1).tags._padding, 100)

        tags = AIFF(self.filename_1)
        self.assertRaises(AIFFError, tags.save, padding=lambda x: -1)
Exemplo n.º 3
0
class TAIFF(TestCase):
    silence_1 = os.path.join(DATA_DIR, '11k-1ch-2s-silence.aif')
    silence_2 = os.path.join(DATA_DIR, '48k-2ch-s16-silence.aif')
    silence_3 = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif')
    silence_4 = os.path.join(DATA_DIR, '8k-1ch-3.5s-silence.aif')
    silence_5 = os.path.join(DATA_DIR, '8k-4ch-1s-silence.aif')

    has_tags = os.path.join(DATA_DIR, 'with-id3.aif')
    no_tags = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif')

    def setUp(self):
        self.filename_1 = get_temp_copy(self.has_tags)
        self.filename_2 = get_temp_copy(self.no_tags)

        self.aiff_tmp_id3 = AIFF(self.filename_1)
        self.aiff_tmp_no_id3 = AIFF(self.filename_2)

        self.aiff_1 = AIFF(self.silence_1)
        self.aiff_2 = AIFF(self.silence_2)
        self.aiff_3 = AIFF(self.silence_3)
        self.aiff_4 = AIFF(self.silence_4)
        self.aiff_5 = AIFF(self.silence_5)

    def test_channels(self):
        self.failUnlessEqual(self.aiff_1.info.channels, 1)
        self.failUnlessEqual(self.aiff_2.info.channels, 2)
        self.failUnlessEqual(self.aiff_3.info.channels, 1)
        self.failUnlessEqual(self.aiff_4.info.channels, 1)
        self.failUnlessEqual(self.aiff_5.info.channels, 4)

    def test_length(self):
        self.failUnlessEqual(self.aiff_1.info.length, 2)
        self.failUnlessEqual(self.aiff_2.info.length, 0.1)
        self.failUnlessEqual(self.aiff_3.info.length, 1)
        self.failUnlessEqual(self.aiff_4.info.length, 3.5)
        self.failUnlessEqual(self.aiff_5.info.length, 1)

    def test_bitrate(self):
        self.failUnlessEqual(self.aiff_1.info.bitrate, 176400)
        self.failUnlessEqual(self.aiff_2.info.bitrate, 1536000)
        self.failUnlessEqual(self.aiff_3.info.bitrate, 128000)
        self.failUnlessEqual(self.aiff_4.info.bitrate, 128000)
        self.failUnlessEqual(self.aiff_5.info.bitrate, 512000)

    def test_sample_rate(self):
        self.failUnlessEqual(self.aiff_1.info.sample_rate, 11025)
        self.failUnlessEqual(self.aiff_2.info.sample_rate, 48000)
        self.failUnlessEqual(self.aiff_3.info.sample_rate, 8000)
        self.failUnlessEqual(self.aiff_4.info.sample_rate, 8000)
        self.failUnlessEqual(self.aiff_5.info.sample_rate, 8000)

    def test_bits_per_sample(self):
        self.failUnlessEqual(self.aiff_1.info.bits_per_sample, 16)
        self.failUnlessEqual(self.aiff_2.info.bits_per_sample, 16)
        self.failUnlessEqual(self.aiff_3.info.bits_per_sample, 16)
        self.failUnlessEqual(self.aiff_4.info.bits_per_sample, 16)
        self.failUnlessEqual(self.aiff_5.info.bits_per_sample, 16)

    def test_sample_size(self):
        for test in [
                self.aiff_1, self.aiff_2, self.aiff_3, self.aiff_4, self.aiff_5
        ]:
            info = test.info
            self.failUnlessEqual(info.sample_size, info.bits_per_sample)

    def test_notaiff(self):
        self.failUnlessRaises(AIFFError, AIFF,
                              os.path.join(DATA_DIR, 'empty.ofr'))

    def test_pprint(self):
        self.failUnless(self.aiff_1.pprint())
        self.failUnless(self.aiff_tmp_id3.pprint())

    def test_delete(self):
        self.aiff_tmp_id3.delete()
        self.failIf(self.aiff_tmp_id3.tags)
        self.failUnless(AIFF(self.filename_1).tags is None)

    def test_module_delete(self):
        delete(self.filename_1)
        self.failUnless(AIFF(self.filename_1).tags is None)

    def test_module_double_delete(self):
        delete(self.filename_1)
        delete(self.filename_1)

    def test_pprint_no_tags(self):
        self.aiff_tmp_id3.tags = None
        self.failUnless(self.aiff_tmp_id3.pprint())

    def test_save_no_tags(self):
        self.aiff_tmp_id3.tags = None
        self.aiff_tmp_id3.save()
        self.assertTrue(self.aiff_tmp_id3.tags is None)

    def test_add_tags_already_there(self):
        self.failUnless(self.aiff_tmp_id3.tags)
        self.failUnlessRaises(Exception, self.aiff_tmp_id3.add_tags)

    def test_mime(self):
        self.failUnless("audio/aiff" in self.aiff_1.mime)
        self.failUnless("audio/x-aiff" in self.aiff_1.mime)

    def test_loaded_tags(self):
        self.failUnless(self.aiff_tmp_id3["TIT2"] == "AIFF title")

    def test_roundtrip(self):
        self.failUnlessEqual(self.aiff_tmp_id3["TIT2"], ["AIFF title"])
        self.aiff_tmp_id3.save()
        new = AIFF(self.aiff_tmp_id3.filename)
        self.failUnlessEqual(new["TIT2"], ["AIFF title"])

    def test_save_tags(self):
        from mutagen.id3 import TIT1
        tags = self.aiff_tmp_id3.tags
        tags.add(TIT1(encoding=3, text="foobar"))
        tags.save()

        new = AIFF(self.aiff_tmp_id3.filename)
        self.failUnlessEqual(new["TIT1"], ["foobar"])

    def test_save_with_ID3_chunk(self):
        from mutagen.id3 import TIT1
        self.aiff_tmp_id3["TIT1"] = TIT1(encoding=3, text="foobar")
        self.aiff_tmp_id3.save()
        self.failUnless(AIFF(self.filename_1)["TIT1"] == "foobar")
        self.failUnless(self.aiff_tmp_id3["TIT2"] == "AIFF title")

    def test_save_without_ID3_chunk(self):
        from mutagen.id3 import TIT1
        self.aiff_tmp_no_id3["TIT1"] = TIT1(encoding=3, text="foobar")
        self.aiff_tmp_no_id3.save()
        self.failUnless(AIFF(self.filename_2)["TIT1"] == "foobar")

    def test_corrupt_tag(self):
        with open(self.filename_1, "r+b") as h:
            chunk = IFFFile(h)[u'ID3']
            h.seek(chunk.data_offset)
            h.seek(4, 1)
            h.write(b"\xff\xff")
        self.assertRaises(AIFFError, AIFF, self.filename_1)

    def test_padding(self):
        AIFF(self.filename_1).save()
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1002)
        AIFF(self.filename_1).save()
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1002)

        tags = AIFF(self.filename_1)
        tags.save(padding=lambda x: 1)
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1)

        tags = AIFF(self.filename_1)
        tags.save(padding=lambda x: 100)
        self.assertEqual(AIFF(self.filename_1).tags._padding, 100)

        tags = AIFF(self.filename_1)
        self.assertRaises(AIFFError, tags.save, padding=lambda x: -1)

    def tearDown(self):
        os.unlink(self.filename_1)
        os.unlink(self.filename_2)
Exemplo n.º 4
0
class TAIFF(TestCase):
    silence_1 = os.path.join(DATA_DIR, '11k-1ch-2s-silence.aif')
    silence_2 = os.path.join(DATA_DIR, '48k-2ch-s16-silence.aif')
    silence_3 = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif')
    silence_4 = os.path.join(DATA_DIR, '8k-1ch-3.5s-silence.aif')
    silence_5 = os.path.join(DATA_DIR, '8k-4ch-1s-silence.aif')

    has_tags = os.path.join(DATA_DIR, 'with-id3.aif')
    no_tags = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif')

    def setUp(self):
        self.filename_1 = get_temp_copy(self.has_tags)
        self.filename_2 = get_temp_copy(self.no_tags)

        self.aiff_tmp_id3 = AIFF(self.filename_1)
        self.aiff_tmp_no_id3 = AIFF(self.filename_2)

        self.aiff_1 = AIFF(self.silence_1)
        self.aiff_2 = AIFF(self.silence_2)
        self.aiff_3 = AIFF(self.silence_3)
        self.aiff_4 = AIFF(self.silence_4)
        self.aiff_5 = AIFF(self.silence_5)

    def test_channels(self):
        self.failUnlessEqual(self.aiff_1.info.channels, 1)
        self.failUnlessEqual(self.aiff_2.info.channels, 2)
        self.failUnlessEqual(self.aiff_3.info.channels, 1)
        self.failUnlessEqual(self.aiff_4.info.channels, 1)
        self.failUnlessEqual(self.aiff_5.info.channels, 4)

    def test_length(self):
        self.failUnlessEqual(self.aiff_1.info.length, 2)
        self.failUnlessEqual(self.aiff_2.info.length, 0.1)
        self.failUnlessEqual(self.aiff_3.info.length, 1)
        self.failUnlessEqual(self.aiff_4.info.length, 3.5)
        self.failUnlessEqual(self.aiff_5.info.length, 1)

    def test_bitrate(self):
        self.failUnlessEqual(self.aiff_1.info.bitrate, 176400)
        self.failUnlessEqual(self.aiff_2.info.bitrate, 1536000)
        self.failUnlessEqual(self.aiff_3.info.bitrate, 128000)
        self.failUnlessEqual(self.aiff_4.info.bitrate, 128000)
        self.failUnlessEqual(self.aiff_5.info.bitrate, 512000)

    def test_sample_rate(self):
        self.failUnlessEqual(self.aiff_1.info.sample_rate, 11025)
        self.failUnlessEqual(self.aiff_2.info.sample_rate, 48000)
        self.failUnlessEqual(self.aiff_3.info.sample_rate, 8000)
        self.failUnlessEqual(self.aiff_4.info.sample_rate, 8000)
        self.failUnlessEqual(self.aiff_5.info.sample_rate, 8000)

    def test_sample_size(self):
        self.failUnlessEqual(self.aiff_1.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_2.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_3.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_4.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_5.info.sample_size, 16)

    def test_notaiff(self):
        self.failUnlessRaises(
            AIFFError, AIFF, os.path.join(DATA_DIR, 'empty.ofr'))

    def test_pprint(self):
        self.failUnless(self.aiff_1.pprint())
        self.failUnless(self.aiff_tmp_id3.pprint())

    def test_delete(self):
        self.aiff_tmp_id3.delete()
        self.failIf(self.aiff_tmp_id3.tags)
        self.failUnless(AIFF(self.filename_1).tags is None)

    def test_module_delete(self):
        delete(self.filename_1)
        self.failUnless(AIFF(self.filename_1).tags is None)

    def test_module_double_delete(self):
        delete(self.filename_1)
        delete(self.filename_1)

    def test_pprint_no_tags(self):
        self.aiff_tmp_id3.tags = None
        self.failUnless(self.aiff_tmp_id3.pprint())

    def test_save_no_tags(self):
        self.aiff_tmp_id3.tags = None
        self.aiff_tmp_id3.save()
        self.assertTrue(self.aiff_tmp_id3.tags is None)

    def test_add_tags_already_there(self):
        self.failUnless(self.aiff_tmp_id3.tags)
        self.failUnlessRaises(Exception, self.aiff_tmp_id3.add_tags)

    def test_mime(self):
        self.failUnless("audio/aiff" in self.aiff_1.mime)
        self.failUnless("audio/x-aiff" in self.aiff_1.mime)

    def test_loaded_tags(self):
        self.failUnless(self.aiff_tmp_id3["TIT2"] == "AIFF title")

    def test_roundtrip(self):
        self.failUnlessEqual(self.aiff_tmp_id3["TIT2"], ["AIFF title"])
        self.aiff_tmp_id3.save()
        new = AIFF(self.aiff_tmp_id3.filename)
        self.failUnlessEqual(new["TIT2"], ["AIFF title"])

    def test_save_tags(self):
        from mutagen.id3 import TIT1
        tags = self.aiff_tmp_id3.tags
        tags.add(TIT1(encoding=3, text="foobar"))
        tags.save()

        new = AIFF(self.aiff_tmp_id3.filename)
        self.failUnlessEqual(new["TIT1"], ["foobar"])

    def test_save_with_ID3_chunk(self):
        from mutagen.id3 import TIT1
        self.aiff_tmp_id3["TIT1"] = TIT1(encoding=3, text="foobar")
        self.aiff_tmp_id3.save()
        self.failUnless(AIFF(self.filename_1)["TIT1"] == "foobar")
        self.failUnless(self.aiff_tmp_id3["TIT2"] == "AIFF title")

    def test_save_without_ID3_chunk(self):
        from mutagen.id3 import TIT1
        self.aiff_tmp_no_id3["TIT1"] = TIT1(encoding=3, text="foobar")
        self.aiff_tmp_no_id3.save()
        self.failUnless(AIFF(self.filename_2)["TIT1"] == "foobar")

    def test_corrupt_tag(self):
        with open(self.filename_1, "r+b") as h:
            chunk = IFFFile(h)[u'ID3']
            h.seek(chunk.data_offset)
            h.seek(4, 1)
            h.write(b"\xff\xff")
        self.assertRaises(AIFFError, AIFF, self.filename_1)

    def test_padding(self):
        AIFF(self.filename_1).save()
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1002)
        AIFF(self.filename_1).save()
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1002)

        tags = AIFF(self.filename_1)
        tags.save(padding=lambda x: 1)
        self.assertEqual(AIFF(self.filename_1).tags._padding, 1)

        tags = AIFF(self.filename_1)
        tags.save(padding=lambda x: 100)
        self.assertEqual(AIFF(self.filename_1).tags._padding, 100)

        tags = AIFF(self.filename_1)
        self.assertRaises(AIFFError, tags.save, padding=lambda x: -1)

    def tearDown(self):
        os.unlink(self.filename_1)
        os.unlink(self.filename_2)
Exemplo n.º 5
0
class TAIFF(TestCase):
    silence_1 = os.path.join('tests', 'data', '11k-1ch-2s-silence.aif')
    silence_2 = os.path.join('tests', 'data', '48k-2ch-s16-silence.aif')
    silence_3 = os.path.join('tests', 'data', '8k-1ch-1s-silence.aif')
    silence_4 = os.path.join('tests', 'data', '8k-1ch-3.5s-silence.aif')
    silence_5 = os.path.join('tests', 'data', '8k-4ch-1s-silence.aif')

    has_tags = os.path.join('tests', 'data', 'with-id3.aif')
    no_tags = os.path.join('tests', 'data', '8k-1ch-1s-silence.aif')

    def setUp(self):
        fd, self.filename_1 = mkstemp(suffix='.aif')
        os.close(fd)
        shutil.copy(self.has_tags, self.filename_1)

        fd, self.filename_2 = mkstemp(suffix='.aif')
        os.close(fd)
        shutil.copy(self.no_tags, self.filename_2)

        self.aiff_tmp_id3 = AIFF(self.filename_1)
        self.aiff_tmp_no_id3 = AIFF(self.filename_2)

        self.aiff_1 = AIFF(self.silence_1)
        self.aiff_2 = AIFF(self.silence_2)
        self.aiff_3 = AIFF(self.silence_3)
        self.aiff_4 = AIFF(self.silence_4)
        self.aiff_5 = AIFF(self.silence_5)

    def test_channels(self):
        self.failUnlessEqual(self.aiff_1.info.channels, 1)
        self.failUnlessEqual(self.aiff_2.info.channels, 2)
        self.failUnlessEqual(self.aiff_3.info.channels, 1)
        self.failUnlessEqual(self.aiff_4.info.channels, 1)
        self.failUnlessEqual(self.aiff_5.info.channels, 4)

    def test_length(self):
        self.failUnlessEqual(self.aiff_1.info.length, 2)
        self.failUnlessEqual(self.aiff_2.info.length, 0.1)
        self.failUnlessEqual(self.aiff_3.info.length, 1)
        self.failUnlessEqual(self.aiff_4.info.length, 3.5)
        self.failUnlessEqual(self.aiff_5.info.length, 1)

    def test_bitrate(self):
        self.failUnlessEqual(self.aiff_1.info.bitrate, 176400)
        self.failUnlessEqual(self.aiff_2.info.bitrate, 1536000)
        self.failUnlessEqual(self.aiff_3.info.bitrate, 128000)
        self.failUnlessEqual(self.aiff_4.info.bitrate, 128000)
        self.failUnlessEqual(self.aiff_5.info.bitrate, 512000)

    def test_sample_rate(self):
        self.failUnlessEqual(self.aiff_1.info.sample_rate, 11025)
        self.failUnlessEqual(self.aiff_2.info.sample_rate, 48000)
        self.failUnlessEqual(self.aiff_3.info.sample_rate, 8000)
        self.failUnlessEqual(self.aiff_4.info.sample_rate, 8000)
        self.failUnlessEqual(self.aiff_5.info.sample_rate, 8000)

    def test_sample_size(self):
        self.failUnlessEqual(self.aiff_1.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_2.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_3.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_4.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_5.info.sample_size, 16)

    def test_notaiff(self):
        self.failUnlessRaises(
            AIFFError, AIFF, os.path.join('tests', 'data', 'empty.ofr'))

    def test_pprint(self):
        self.failUnless(self.aiff_1.pprint())
        self.failUnless(self.aiff_tmp_id3.pprint())

    def test_delete(self):
        self.aiff_tmp_id3.delete()
        self.failIf(self.aiff_tmp_id3.tags)
        self.failUnless(AIFF(self.filename_1).tags is None)

    def test_module_delete(self):
        delete(self.filename_1)
        self.failUnless(AIFF(self.filename_1).tags is None)

    def test_module_double_delete(self):
        delete(self.filename_1)
        delete(self.filename_1)

    def test_pprint_no_tags(self):
        self.aiff_tmp_id3.tags = None
        self.failUnless(self.aiff_tmp_id3.pprint())

    def test_save_no_tags(self):
        self.aiff_tmp_id3.tags = None
        self.failUnlessRaises(ValueError, self.aiff_tmp_id3.save)

    def test_add_tags_already_there(self):
        self.failUnless(self.aiff_tmp_id3.tags)
        self.failUnlessRaises(Exception, self.aiff_tmp_id3.add_tags)

    def test_mime(self):
        self.failUnless("audio/aiff" in self.aiff_1.mime)
        self.failUnless("audio/x-aiff" in self.aiff_1.mime)

    def test_loaded_tags(self):
        self.failUnless(self.aiff_tmp_id3["TIT2"] == "AIFF title")

    def test_roundtrip(self):
        self.failUnlessEqual(self.aiff_tmp_id3["TIT2"], ["AIFF title"])
        self.aiff_tmp_id3.save()
        new = AIFF(self.aiff_tmp_id3.filename)
        self.failUnlessEqual(new["TIT2"], ["AIFF title"])

    def test_save_tags(self):
        from mutagen.id3 import TIT1
        tags = self.aiff_tmp_id3.tags
        tags.add(TIT1(encoding=3, text="foobar"))
        tags.save()

        new = AIFF(self.aiff_tmp_id3.filename)
        self.failUnlessEqual(new["TIT1"], ["foobar"])

    def test_save_with_ID3_chunk(self):
        from mutagen.id3 import TIT1
        self.aiff_tmp_id3["TIT1"] = TIT1(encoding=3, text="foobar")
        self.aiff_tmp_id3.save()
        self.failUnless(AIFF(self.filename_1)["TIT1"] == "foobar")
        self.failUnless(self.aiff_tmp_id3["TIT2"] == "AIFF title")

    def test_save_without_ID3_chunk(self):
        from mutagen.id3 import TIT1
        self.aiff_tmp_no_id3["TIT1"] = TIT1(encoding=3, text="foobar")
        self.aiff_tmp_no_id3.save()
        self.failUnless(AIFF(self.filename_2)["TIT1"] == "foobar")

    def tearDown(self):
        os.unlink(self.filename_1)
        os.unlink(self.filename_2)
Exemplo n.º 6
0
    def update_id3(self, path: str, track: beatport.Track):
        #AIFF Check
        aiff = None
        if path.endswith('.aiff') or path.endswith('.aif'):
            aiff = AIFF(path)
            f = aiff.tags
        else:
            f = ID3()
            f.load(path, v2_version=3, translate=True)

        #Update tags
        if UpdatableTags.title in self.config.update_tags and self.config.overwrite:
            f.setall('TIT2', [TIT2(text=track.title)])
        if UpdatableTags.artist in self.config.update_tags and self.config.overwrite:
            f.setall('TPE1', [
                TPE1(text=self.config.artist_separator.join(
                    [a.name for a in track.artists]))
            ])
        if UpdatableTags.album in self.config.update_tags and (
                self.config.overwrite or len(f.getall('TALB')) == 0):
            f.setall('TALB', [TALB(text=track.album.name)])
        if UpdatableTags.label in self.config.update_tags and (
                self.config.overwrite or len(f.getall('TPUB')) == 0):
            f.setall('TPUB', [TPUB(text=track.label.name)])
        if UpdatableTags.bpm in self.config.update_tags and (
                self.config.overwrite or len(f.getall('TBPM')) == 0):
            f.setall('TBPM', [TBPM(text=str(track.bpm))])
        if UpdatableTags.genre in self.config.update_tags and (
                self.config.overwrite or len(f.getall('TCON')) == 0):
            f.setall('TCON',
                     [TCON(text=', '.join([g.name for g in track.genres]))])

        #Dates
        if UpdatableTags.date in self.config.update_tags:
            #ID3 v2.3
            if self.config.id3v23 and (self.config.overwrite or
                                       (len(f.getall('TYER')) == 0
                                        and len(f.getall('TDAT')) == 0)):
                date = track.release_date.strftime('%d%m')
                f.setall('TDRC', [])
                f.setall('TDAT', [TDAT(text=date)])
                f.setall('TYER', [TYER(text=str(track.release_date.year))])
            #ID3 v2.4
            if not self.config.id3v23 and (self.config.overwrite
                                           or len(f.getall('TDRC')) == 0):
                date = track.release_date.strftime('%Y-%m-%d')
                f.setall('TDAT', [])
                f.setall('TYER', [])
                f.setall('TDRC', [TDRC(text=date)])

        if UpdatableTags.key in self.config.update_tags and (
                self.config.overwrite or len(f.getall('TKEY')) == 0):
            f.setall('TKEY', [TKEY(text=track.id3key())])
        if UpdatableTags.publishdate in self.config.update_tags and (
                self.config.overwrite or len(f.getall('TDRL')) == 0):
            # f.setall('TORY', [TORY(text=str(track.publish_date.year))])
            if not self.config.id3v23:
                date = track.publish_date.strftime('%Y-%m-%d')
                f.setall('TDRL', [TDRL(text=date)])

        #Other keys
        if UpdatableTags.other in self.config.update_tags:
            f.add(TXXX(desc='WWWAUDIOFILE', text=track.url()))
            f.add(TXXX(desc='WWWPUBLISHER', text=track.label.url('label')))

        #Redownlaod cover
        if self.config.replace_art:
            try:
                url = track.art(self.config.art_resolution)
                r = requests.get(url)
                data = APIC(encoding=3,
                            mime='image/jpeg',
                            type=3,
                            desc=u'Cover',
                            data=r.content)
                f.delall('APIC')
                f['APIC:cover.jpg'] = data

            except Exception:
                logging.warning('Error downloading cover for file: ' + path)

        if aiff == None:
            if self.config.id3v23:
                f.save(path, v2_version=3, v1=0)
            else:
                f.save(path, v2_version=4, v1=0)
        else:
            aiff.save()
def initiateAIFF(filename, directory, thumbnails, options):
    audio = AIFF(str(directory) + "/" + str(filename))
    # verify artist information is present before preceeding
    if ' - ' not in filename and str(audio['TCON']) == '':
        messagebox.showinfo("No artist information found, aborting procedure")
        return False, False, False

    # transcribe formal tagnames into informal counterpart
    formalTagDict = {
        'TPE1': 'Artist',
        'TALB': 'Album',
        'TPE2': 'Album_Artist',
        'TBPM': 'BPM',
        'COMM::eng': 'Comment',
        'TCMP': 'Compilation',
        'TCOP': 'Copyright',
        'TPOS': 'Discnumber',
        'TCON': 'Genre',
        'APIC:': 'Image',
        'TKEY': 'Key',
        'TDRC': 'Release_Date',
        'TIT2': 'Title',
        'TXXX:replaygain_track_gain': 'ReplayGain',
    }
    # transcribe informal tagnames into formal counterpart
    informalTagDict = {v: k for k, v in formalTagDict.items()}

    ID3Frames = {
        'TPE1': TPE1,
        'TALB': TALB,
        'TPE2': TPE2,
        'TBPM': TBPM,
        'COMM': COMM,
        'TCMP': TCMP,
        'TCOP': TCOP,
        'TPOS': TPOS,
        'TCON': TCON,
        'APIC:': APIC,
        'TKEY': TKEY,
        'TDRC': TDRC,
        'TIT2': TIT2,
        'TXXX': TXXX,
    }
    fileParameters = []
    tagList = list(audio.keys())
    for tag in tagList:
        # delete extraneous tags if the tag is not in the list of selected tags and the delete unselected tags option is activated
        if (tag not in formalTagDict
                or formalTagDict[tag] not in options["Selected Tags (L)"]
            ) and options["Delete Unselected Tags (B)"].get() == True:
            audio.pop(tag)
            audio.save()
        else:
            fileParameters.append(tag)
    for tag in options["Selected Tags (L)"]:
        if tag in informalTagDict:
            tag = informalTagDict[tag]
            # add tags of interest if missing
            if tag not in fileParameters:
                try:
                    if "COMM" in tag:
                        audio[tag] = COMM(encoding=3, lang="eng", text="")
                    elif "TXXX" in tag:
                        audio[tag] = TXXX(encoding=3,
                                          desc="replaygain_track_gain",
                                          text="")
                    else:
                        audio[tag] = ID3Frames[tag](encoding=3, text="")
                    audio.save()
                except:
                    messagebox.showinfo(
                        "Permission Error",
                        "Unable to save tags, file may be open somewhere")
                    return False, False, False

    # check for discrepancies between tags and filename
    # check both artist and title tags
    if ' - ' in filename:
        artist = filename.split(' - ')[0]
        title = filename[filename.index(filename.split(' - ')[1]):filename.
                         rfind('.')]
        if artist != str(audio["TPE1"]) or title != str(audio["TIT2"]):
            # save artist and title to tag if both are empty
            if str(audio["TPE1"]) == '' and str(audio["TIT2"]) == '':
                audio["TPE1"] = TPE1(encoding=3, text=artist)
                audio["TIT2"] = TIT2(encoding=3, text=title)
                audio.save()
            else:
                audio, filename = compareArtistAndTitle(
                    audio, artist, title, filename, directory, options)
    # only check title tag
    else:
        title = filename[:filename.rfind('.')]
        if title != str(audio["TIT2"]):
            # save title to tag if tag is empty
            if str(audio["TIT2"]) == '':
                audio["TIT2"] = TIT2(encoding=3, text=title)
                audio.save()
            else:
                audio, filename = compareTitle(audio, title, filename,
                                               directory, options)

    # handle naming format and typo check
    if options["Audio naming format (S)"].get() == "Artist - Title" or options[
            'Audio naming format (S)'].get() == 'Title':
        namingConvention = options['Audio naming format (S)'].get()
        artist = str(audio["TPE1"])
        audio, filename = handleStaticNamingConvention(audio, filename, artist,
                                                       title, directory,
                                                       namingConvention)
        if options["Scan Filename and Tags (B)"].get(
        ) == True and type(audio) != bool:
            audio, filename, options = extractArtistAndTitle(
                audio, filename, directory, options, namingConvention)

    if type(audio) != bool:
        # save thumbnail to list
        image = audio["APIC:"]
        if image.data != b'':
            stream = BytesIO(image.data)
            image = Image.open(stream).convert("RGBA")
            thumbnails = saveThumbnail(image, thumbnails)
            stream.close()
        else:
            thumbnails = saveThumbnail("NA", thumbnails)
    return audio, filename, informalTagDict, thumbnails, options
Exemplo n.º 8
0
class TAIFF(TestCase):
    silence_1 = os.path.join('tests', 'data', '11k-1ch-2s-silence.aif')
    silence_2 = os.path.join('tests', 'data', '48k-2ch-s16-silence.aif')
    silence_3 = os.path.join('tests', 'data', '8k-1ch-1s-silence.aif')
    silence_4 = os.path.join('tests', 'data', '8k-1ch-3.5s-silence.aif')
    silence_5 = os.path.join('tests', 'data', '8k-4ch-1s-silence.aif')

    has_tags = os.path.join('tests', 'data', 'with-id3.aif')
    no_tags = os.path.join('tests', 'data', '8k-1ch-1s-silence.aif')

    def setUp(self):
        fd, self.filename_1 = mkstemp(suffix='.aif')
        os.close(fd)
        shutil.copy(self.has_tags, self.filename_1)

        fd, self.filename_2 = mkstemp(suffix='.aif')
        os.close(fd)
        shutil.copy(self.no_tags, self.filename_2)

        self.aiff_tmp_id3 = AIFF(self.filename_1)
        self.aiff_tmp_no_id3 = AIFF(self.filename_2)

        self.aiff_1 = AIFF(self.silence_1)
        self.aiff_2 = AIFF(self.silence_2)
        self.aiff_3 = AIFF(self.silence_3)
        self.aiff_4 = AIFF(self.silence_4)
        self.aiff_5 = AIFF(self.silence_5)

    def test_channels(self):
        self.failUnlessEqual(self.aiff_1.info.channels, 1)
        self.failUnlessEqual(self.aiff_2.info.channels, 2)
        self.failUnlessEqual(self.aiff_3.info.channels, 1)
        self.failUnlessEqual(self.aiff_4.info.channels, 1)
        self.failUnlessEqual(self.aiff_5.info.channels, 4)

    def test_length(self):
        self.failUnlessEqual(self.aiff_1.info.length, 2)
        self.failUnlessEqual(self.aiff_2.info.length, 0.1)
        self.failUnlessEqual(self.aiff_3.info.length, 1)
        self.failUnlessEqual(self.aiff_4.info.length, 3.5)
        self.failUnlessEqual(self.aiff_5.info.length, 1)

    def test_bitrate(self):
        self.failUnlessEqual(self.aiff_1.info.bitrate, 176400)
        self.failUnlessEqual(self.aiff_2.info.bitrate, 1536000)
        self.failUnlessEqual(self.aiff_3.info.bitrate, 128000)
        self.failUnlessEqual(self.aiff_4.info.bitrate, 128000)
        self.failUnlessEqual(self.aiff_5.info.bitrate, 512000)

    def test_sample_rate(self):
        self.failUnlessEqual(self.aiff_1.info.sample_rate, 11025)
        self.failUnlessEqual(self.aiff_2.info.sample_rate, 48000)
        self.failUnlessEqual(self.aiff_3.info.sample_rate, 8000)
        self.failUnlessEqual(self.aiff_4.info.sample_rate, 8000)
        self.failUnlessEqual(self.aiff_5.info.sample_rate, 8000)

    def test_sample_size(self):
        self.failUnlessEqual(self.aiff_1.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_2.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_3.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_4.info.sample_size, 16)
        self.failUnlessEqual(self.aiff_5.info.sample_size, 16)

    def test_notaiff(self):
        self.failUnlessRaises(AIFFError, AIFF,
                              os.path.join('tests', 'data', 'empty.ofr'))

    def test_pprint(self):
        self.failUnless(self.aiff_1.pprint())
        self.failUnless(self.aiff_tmp_id3.pprint())

    def test_delete(self):
        self.aiff_tmp_id3.delete()
        self.failIf(self.aiff_tmp_id3.tags)
        self.failUnless(AIFF(self.filename_1).tags is None)

    def test_module_delete(self):
        delete(self.filename_1)
        self.failUnless(AIFF(self.filename_1).tags is None)

    def test_module_double_delete(self):
        delete(self.filename_1)
        delete(self.filename_1)

    def test_pprint_no_tags(self):
        self.aiff_tmp_id3.tags = None
        self.failUnless(self.aiff_tmp_id3.pprint())

    def test_save_no_tags(self):
        self.aiff_tmp_id3.tags = None
        self.failUnlessRaises(ValueError, self.aiff_tmp_id3.save)

    def test_add_tags_already_there(self):
        self.failUnless(self.aiff_tmp_id3.tags)
        self.failUnlessRaises(Exception, self.aiff_tmp_id3.add_tags)

    def test_mime(self):
        self.failUnless("audio/aiff" in self.aiff_1.mime)
        self.failUnless("audio/x-aiff" in self.aiff_1.mime)

    def test_loaded_tags(self):
        self.failUnless(self.aiff_tmp_id3["TIT2"] == "AIFF title")

    def test_roundtrip(self):
        self.failUnlessEqual(self.aiff_tmp_id3["TIT2"], ["AIFF title"])
        self.aiff_tmp_id3.save()
        new = AIFF(self.aiff_tmp_id3.filename)
        self.failUnlessEqual(new["TIT2"], ["AIFF title"])

    def test_save_tags(self):
        from mutagen.id3 import TIT1
        tags = self.aiff_tmp_id3.tags
        tags.add(TIT1(encoding=3, text="foobar"))
        tags.save()

        new = AIFF(self.aiff_tmp_id3.filename)
        self.failUnlessEqual(new["TIT1"], ["foobar"])

    def test_save_with_ID3_chunk(self):
        from mutagen.id3 import TIT1
        self.aiff_tmp_id3["TIT1"] = TIT1(encoding=3, text="foobar")
        self.aiff_tmp_id3.save()
        self.failUnless(AIFF(self.filename_1)["TIT1"] == "foobar")
        self.failUnless(self.aiff_tmp_id3["TIT2"] == "AIFF title")

    def test_save_without_ID3_chunk(self):
        from mutagen.id3 import TIT1
        self.aiff_tmp_no_id3["TIT1"] = TIT1(encoding=3, text="foobar")
        self.aiff_tmp_no_id3.save()
        self.failUnless(AIFF(self.filename_2)["TIT1"] == "foobar")

    def tearDown(self):
        os.unlink(self.filename_1)
        os.unlink(self.filename_2)