Пример #1
0
def writeMeta(filename,meta,verbose=True):
    '''Write ID3 metadata to audio file

    <filename>: str, abspath to audio file.
    <meta>: dict, metadata dict.

    Write metadata into the .mp3 audio file using ID3

    Update time: 2016-07-12 14:09:27.
    '''

    from mutagen.mp3 import MP3
    from mutagen.mp4 import MP4, MP4Cover
    from mutagen.id3 import ID3, APIC, error

    audio=mutagen.File(filename)

    #------------Add ID3 tag if not exists------------
    try:
        audio.add_tags()
    except:
        pass

    for kk,vv in meta.items():
        if kk=='cover':
            #------------------For mp3 format------------------
            #ap=APIC(encoding=3, mime=tools.deu(vv),type=3,\
            #desc=u'Cover',data=open(vv,'rb').read())
            #audio.tags.add(ap)

            #------------------For mp4 format------------------
            ap2=MP4Cover(open(vv,'rb').read(),\
                imageformat=MP4Cover.FORMAT_JPEG)
            audio['covr']=[ap2]
        else:
            audio[kk]=tools.deu(vv)
    
    audio.save()

    return
Пример #2
0
def writeMeta(filename, meta, verbose=True):
    '''Write ID3 metadata to audio file

    <filename>: str, abspath to audio file.
    <meta>: dict, metadata dict.

    Write metadata into the .mp3 audio file using ID3

    Update time: 2016-07-12 14:09:27.
    '''

    from mutagen.mp3 import MP3
    from mutagen.mp4 import MP4, MP4Cover
    from mutagen.id3 import ID3, APIC, error

    audio = mutagen.File(filename)

    #------------Add ID3 tag if not exists------------
    try:
        audio.add_tags()
    except:
        pass

    for kk, vv in meta.items():
        if kk == 'cover':
            #------------------For mp3 format------------------
            #ap=APIC(encoding=3, mime=tools.deu(vv),type=3,\
            #desc=u'Cover',data=open(vv,'rb').read())
            #audio.tags.add(ap)

            #------------------For mp4 format------------------
            ap2=MP4Cover(open(vv,'rb').read(),\
                imageformat=MP4Cover.FORMAT_JPEG)
            audio['covr'] = [ap2]
        else:
            audio[kk] = tools.deu(vv)

    audio.save()

    return
Пример #3
0
def processAlbum(df,indir,outdir,albumid,verbose=True):
    '''Process files in an album

    '''
    seldf=df[df.albumId==albumid]
    albumname=seldf.iloc[0].albumName
    ids=seldf.rowid
    faillist=[]
    metafaillist=[]

    subfolder=os.path.join(outdir,albumname)
    subfolder=convertPath(subfolder)
    if not os.path.isdir(subfolder):
        try:
            os.makedirs(subfolder)
        except:
            if verbose:
                printInd('Failed to create subfolder %s' %albumname,2)
                printInd('Skip folder %s' %albumname,2)
            faillist.extend(fetchField(df,'title'))
            return faillist,metafaillist

    #------------Download album cover image------------
    albumImage=seldf.iloc[0].albumImage
    try:
        coverimg=os.path.join(subfolder,'cover.jpg')
        imgfile=urlretrieve(albumImage,coverimg)[0]
        got_cover=True
    except:
        got_cover=False

    #----------------Loop through files----------------
    for ii in range(len(ids)):
        title=seldf.iloc[ii].title
        artist=seldf.iloc[ii].artist
        downloaded=seldf.iloc[ii].downloaded
        totalBytes=seldf.iloc[ii].totalBytes
        downloadurl1=seldf.iloc[ii].downloadUrl
        downloadurl2=seldf.iloc[ii].downloadAacUrl
        filepath=seldf.iloc[ii].filepath

        tmpfile=False
        gotfile=False

        newname="%s-%s.mp4" %(title,artist)
	newname=REPATTERN.sub(' ',newname)
        newname=os.path.join(tools.deu(subfolder),newname)
        newname=convertPath(newname)

        if verbose:
            #printInd('Getting file for: %s' %title, 2)
            printInd(dgbk('获取文件: ')+title, 2)

        #-----If imcomplete download, try downloading now-----
        if downloaded<totalBytes:
            tmpfile=True
            if verbose:
                printInd('Downloading imcomplete audio:',2)
                printInd(title,2)
            try:
                tmpfile=urlretrieve(downloadurl1,newname)
                gotfile=True
            except:
                tmpfile=urlretrieve(downloadurl2,newname)
                gotfile=True
            finally:
                if verbose:
                    printInd('Failed to download %s' %title,2)
                faillist.append(title)
                gotfile=False
        else:
            gotfile=True

        if not gotfile:
            continue

        #----------------------Export----------------------
        if not tmpfile:

            filename=os.path.join(indir,'Download')
            filename=os.path.join(filename,filepath)

            if os.path.exists(filename):
                try:
                    shutil.copy2(filename,newname)
                except:
                    if verbose:
                        printInd('Failed to copy file %s' %title,2)
                        faillist.append(title)
                    continue

        #------------Write metadata (optional)------------
        if HAS_MUTAGEN:

            if verbose:
                #printInd('Writing metadata for: %s' %title, 2)
                printInd(dgbk('为音频写入元数据: ')+title, 2)

            #--------------------mp3 format--------------------
            meta={'title':title, 'artist': artist, 'album': albumname,\
                  'comments': 'Exported from Ximalaya by XimaExport'}
            #--------------------mp4 format--------------------
            meta={'\xa9nam':title, '\xa9ART': artist, '\xa9alb': albumname,\
                  'comments': 'Exported from Ximalaya by XimaExport'}
            if got_cover:
                meta['cover']=imgfile

            try:
                writeMeta(newname,meta)
            except:
                metafaillist.append(title)


    return faillist,metafaillist
Пример #4
0
def processAlbum(df, indir, outdir, albumid, verbose=True):
    '''Process files in an album

    '''
    seldf = df[df.albumId == albumid]
    albumname = seldf.iloc[0].albumName
    ids = seldf.rowid
    faillist = []
    metafaillist = []

    subfolder = os.path.join(outdir, albumname)
    subfolder = convertPath(subfolder)
    if not os.path.isdir(subfolder):
        try:
            os.makedirs(subfolder)
        except:
            if verbose:
                printInd('Failed to create subfolder %s' % albumname, 2)
                printInd('Skip folder %s' % albumname, 2)
            faillist.extend(fetchField(df, 'title'))
            return faillist, metafaillist

    #------------Download album cover image------------
    albumImage = seldf.iloc[0].albumImage
    try:
        coverimg = os.path.join(subfolder, 'cover.jpg')
        imgfile = urlretrieve(albumImage, coverimg)[0]
        got_cover = True
    except:
        got_cover = False

    #----------------Loop through files----------------
    for ii in range(len(ids)):
        title = seldf.iloc[ii].title
        artist = seldf.iloc[ii].artist
        downloaded = seldf.iloc[ii].downloaded
        totalBytes = seldf.iloc[ii].totalBytes
        downloadurl1 = seldf.iloc[ii].downloadUrl
        downloadurl2 = seldf.iloc[ii].downloadAacUrl
        filepath = seldf.iloc[ii].filepath

        tmpfile = False
        gotfile = False

        newname = "%s-%s.mp4" % (title, artist)
        newname = REPATTERN.sub(' ', newname)
        newname = os.path.join(tools.deu(subfolder), newname)
        newname = convertPath(newname)

        if verbose:
            #printInd('Getting file for: %s' %title, 2)
            printInd(dgbk('获取文件: ') + title, 2)

        #-----If imcomplete download, try downloading now-----
        if downloaded < totalBytes:
            tmpfile = True
            if verbose:
                printInd('Downloading imcomplete audio:', 2)
                printInd(title, 2)
            try:
                tmpfile = urlretrieve(downloadurl1, newname)
                gotfile = True
            except:
                tmpfile = urlretrieve(downloadurl2, newname)
                gotfile = True
            finally:
                if verbose:
                    printInd('Failed to download %s' % title, 2)
                faillist.append(title)
                gotfile = False
        else:
            gotfile = True

        if not gotfile:
            continue

        #----------------------Export----------------------
        if not tmpfile:

            filename = os.path.join(indir, 'Download')
            filename = os.path.join(filename, filepath)

            if os.path.exists(filename):
                try:
                    shutil.copy2(filename, newname)
                except:
                    if verbose:
                        printInd('Failed to copy file %s' % title, 2)
                        faillist.append(title)
                    continue

        #------------Write metadata (optional)------------
        if HAS_MUTAGEN:

            if verbose:
                #printInd('Writing metadata for: %s' %title, 2)
                printInd(dgbk('为音频写入元数据: ') + title, 2)

            #--------------------mp3 format--------------------
            meta={'title':title, 'artist': artist, 'album': albumname,\
                  'comments': 'Exported from Ximalaya by XimaExport'}
            #--------------------mp4 format--------------------
            meta={'\xa9nam':title, '\xa9ART': artist, '\xa9alb': albumname,\
                  'comments': 'Exported from Ximalaya by XimaExport'}
            if got_cover:
                meta['cover'] = imgfile

            try:
                writeMeta(newname, meta)
            except:
                metafaillist.append(title)

    return faillist, metafaillist