How to use the mutagen.mp4.MP4 function in mutagen

To help you get started, we’ve selected a few mutagen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LordSputnik / mutagen / tests / test_mp4.py View on Github external
def setUp(self):
        fd, self.filename = mkstemp(suffix='.m4a')
        os.close(fd)
        shutil.copy(self.original, self.filename)
        self.audio = MP4(self.filename)
github quodlibet / mutagen / tests / test_mp4.py View on Github external
def test_set_init_padding_large(self):
        if self.audio.tags is None:
            self.audio.add_tags()
        self.audio.save(padding=lambda x: 5000)
        self.assertEqual(MP4(self.audio.filename)._padding, 5000)
github LordSputnik / mutagen / tests / test_mp4.py View on Github external
def test_delete(self):
        self.audio.delete()
        audio = MP4(self.audio.filename)
        self.failIf(audio.tags)
        self.faad()
github hbashton / spotify-ripper / spotify_ripper / tags.py View on Github external
audio = m4a.M4A(audio_file)
                set_m4a_tags(audio)
                audio = mp4.MP4(audio_file)
        elif args.output_type == "alac.m4a":
            if sys.version_info >= (3, 0):
                from mutagen import mp4

                audio = mp4.MP4(audio_file)
                set_mp4_tags(audio)
            else:
                from mutagen import m4a, mp4

                audio = m4a.M4A(audio_file)
                set_m4a_tags(audio)
                audio = mp4.MP4(audio_file)
        elif args.output_type == "mp3":
            audio = mp3.MP3(audio_file, ID3=id3.ID3)
            set_id3_tags(audio)

        def bit_rate_str(bit_rate):
            brs = "%d kb/s" % bit_rate
            if not args.cbr:
                brs = "~" + brs
            return brs

        def mode_str(mode):
            modes = ["Stereo", "Joint Stereo", "Dual Channel", "Mono"]
            if mode < len(modes):
                return modes[mode]
            else:
                return ""
github pigsboss / toolbox / audio / slim.py View on Github external
def save_tags(meta, audio_file):
    """Save metadata to specified audio file.
"""
    if audio_file.lower().endswith('.dsf'):
        audio = DSF(audio_file)
        scheme = 'ID3'
    elif audio_file.lower().endswith('.flac'):
        audio = FLAC(audio_file)
        scheme = 'Vorbis'
    elif audio_file.lower().endswith('.m4a'):
        audio = MP4(audio_file)
        scheme = 'MP4'
    elif audio_file.lower().endswith('.mp3'):
        audio = MP3(audio_file)
        scheme ='ID3'
    else:
        raise TypeError(u'unsupported audio file format {}.'.format(audio_file))
    if scheme == 'ID3':
        for k in meta:
            if k in TAG_MAP[scheme]:
                if k == 'date':
                    audio[TAG_MAP[scheme][k]] = TextFrame(encoding=3, text=[ID3TimeStamp(meta[k])])
                elif k == 'discnumber':
                    if meta['totaldiscs'] > 0:
                        audio[TAG_MAP[scheme][k]] = TextFrame(encoding=3, text=['{:d}/{:d}'.format(meta[k], meta['totaldiscs'])])
                    else:
                        audio[TAG_MAP[scheme][k]] = TextFrame(encoding=3, text=['{:d}'.format(meta[k])])
github pigsboss / toolbox / audio / slim.py View on Github external
def set_source_file_checksum(audio_file, csum, program=DEFAULT_CHECKSUM_PROG):
    if  audio_file.lower().endswith('.flac'):
        metadata = FLAC(audio_file)
        scheme = 'Vorbis'
    elif audio_file.lower().endswith('.dsf'):
        metadata = DSF(audio_file)
        scheme = 'ID3'
    elif audio_file.lower().endswith('.mp3'):
        metadata = MP3(audio_file)
        scheme = 'ID3'
    elif audio_file.lower().endswith('.m4a'):
        metadata = MP4(audio_file)
        scheme = 'MP4'
    else:
        raise TypeError(u'unsupported audio format {}.'.format(audio_file))
    if scheme=='ID3':
        if TAG_MAP[scheme]['comment'] in metadata.tags.keys():
            metadata.tags[TAG_MAP[scheme]['comment']] = COMM(encoding=3, text=['\n'.join([
                metadata.tags[TAG_MAP[scheme]['comment']][0],
                u'Source Checksum Program: {}'.format(program),
                u'Source File Checksum: {}'.format(csum)])])
        else:
            metadata.tags[TAG_MAP[scheme]['comment']] = COMM(encoding=3, text=['\n'.join([
                u'Source Checksum Program: {}'.format(program),
                u'Source File Checksum: {}'.format(csum)])])
    else:
        if TAG_MAP[scheme]['comment'] in metadata.tags.keys():
            if isinstance(metadata.tags[TAG_MAP[scheme]['comment']], str):
github spiderbit / canta / canta / metadata / mp4.py View on Github external
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

from canta.metadata import BaseFormat
from mutagen import mp4

class MP4Format(BaseFormat):
    MutagenType = mp4.MP4
    tag_mapping = {
            'title':       '\xa9nam',
            'artist':      '\xa9ART',
            'album':       '\xa9alb',
            'genre':       '\xa9gen',
            'date':        '\xa9day',
            'tracknumber': 'trkn',
            'discnumber':  'disk',
            'copyright':   'cprt',
        }
    others = False
    writable = True
github moustaki / motools / gnat / src / gnat / MbzTrackLookup.py View on Github external
def __init__(self,file,metadata=None,threshold=60,duration_distance_threshold=10000) :
		self.file = file
		self.threshold = threshold
		self.ddt=duration_distance_threshold
		self.cleaner = re.compile(r"[^A-Za-z0-9 ]").sub
		self.audio = None
		
		if metadata is not None: # We trust provided metadata over local tags
			self.audio = metadata
		else:
			# Try MP3 last cause it doesn't seem to throw an exception on wrong filetypes...
			handlers = [FLAC,OggVorbis,OggFLAC,OggTheora,APEv2,ASF,MP4,Musepack,TrueAudio,WavPack,lambda x: MP3(x,ID3=EasyID3)]
			for handler in handlers:
				if self.audio is None:
					try:
						self.audio = handler(file)
					except:
						pass
			if self.audio is None:
				raise FileTypeException('Unknown file type, no metadata, or file not found.')
			
		try :
			[title] = self.audio['title']
			self.title = self.__clean_literal(str(title))
		except :
			self.title = None
		try :
			[artist] = self.audio['artist']
github Polsaker / throat / app / storage.py View on Github external
def clear_metadata(path: str, mime_type: str):
    if mime_type in ('image/jpeg', 'image/png'):
        exif = GExiv2.Metadata()
        exif.open_path(path)
        exif.clear()
        exif.save_file(path)
    elif mime_type == 'video/mp4':
        video = MP4(path)
        video.clear()
        video.save()
    elif mime_type == 'video/webm':
        # XXX: Mutagen doesn't seem to support webm files
        pass
github MediaBrowser / plugin.video.emby / resources / lib / libraries / mutagen / _file.py View on Github external
if easy:
            from mutagen.trueaudio import EasyTrueAudio as TrueAudio
        else:
            from mutagen.trueaudio import TrueAudio
        from mutagen.wavpack import WavPack
        if easy:
            from mutagen.easymp4 import EasyMP4 as MP4
        else:
            from mutagen.mp4 import MP4
        from mutagen.musepack import Musepack
        from mutagen.monkeysaudio import MonkeysAudio
        from mutagen.optimfrog import OptimFROG
        from mutagen.aiff import AIFF
        from mutagen.aac import AAC
        options = [MP3, TrueAudio, OggTheora, OggSpeex, OggVorbis, OggFLAC,
                   FLAC, AIFF, APEv2File, MP4, ID3FileType, WavPack,
                   Musepack, MonkeysAudio, OptimFROG, ASF, OggOpus, AAC]

    if not options:
        return None

    with open(filename, "rb") as fileobj:
        header = fileobj.read(128)
        # Sort by name after score. Otherwise import order affects
        # Kind sort order, which affects treatment of things with
        # equals scores.
        results = [(Kind.score(filename, fileobj, header), Kind.__name__)
                   for Kind in options]

    results = list(izip(results, options))
    results.sort()
    (score, name), Kind = results[-1]