当前位置: 首页>>代码示例>>Python>>正文


Python File.tags['TPE1']方法代码示例

本文整理汇总了Python中mutagen.File.tags['TPE1']方法的典型用法代码示例。如果您正苦于以下问题:Python File.tags['TPE1']方法的具体用法?Python File.tags['TPE1']怎么用?Python File.tags['TPE1']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mutagen.File的用法示例。


在下文中一共展示了File.tags['TPE1']方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from mutagen import File [as 别名]
# 或者: from mutagen.File import tags['TPE1'] [as 别名]
def main():
    parser = argparse.ArgumentParser('Change or manipulate the ID3 tags of the audio files')
    parser.add_argument('--artist','-a', default='', help='set the artist name')
    parser.add_argument('--album','-A', default='', help='set the album name')
    parser.add_argument('--title','-t', default='', help='set the title name')
    parser.add_argument('--wors','-r', default='', help='set the internet radio url')
    parser.add_argument('--year','-Y', default='', help='set the release year')
    parser.add_argument('--cover','-c', default='', help='set the cover image')
    parser.add_argument('--format', '-f', default='', help='''return the ID3 information as a formatted string;
                                                        the format string should containing one or more of the following specifiers:
                                                        , {artist}
                                                        , {title}
                                                        , {album}
                                                        , {year}
                                                        , {kbps}
                                                        , {wors}
                                                        , {len} (the length of the audio file, in seconds)
                                                        , {path} (the absolute path of the file)''')
    parser.add_argument('--separator','-s', default='\n', help='define the separator used to append at the end of the output for each file (excluding the last file)')
    parser.add_argument('--escape','-e', default='', help='define the characters that should be escaped in all the outputed fields')
    parser.add_argument('audiofile', nargs='+', help='an audio file containing the ID3 tags')

    args = parser.parse_args()

    # input section
    to_artist = unicode(args.artist.decode('utf-8'))
    to_album = unicode(args.album.decode('utf-8'))
    to_title = unicode(args.title.decode('utf-8'))
    to_wors = unicode(args.wors.decode('utf-8'))
    to_year = unicode(args.year.decode('utf-8'))
    to_cover = unicode(args.cover.decode('utf-8'))
    if to_cover != '':
        cover_ext = os.path.splitext(to_cover)[1]
        #print("Cover path is "+to_cover)
        import mimetypes
        mimetypes.init()
        to_cover_mime = mimetypes.types_map[cover_ext]
        #print("Mime type is "+to_cover_mime)

    # output section
    outputs = []
    formatstr = unicode(args.format)
    escapepat = ""
    delimiter = ''
    for c in unicode(args.escape):
        esc = ''
        if c in r'()[]\^$.|?*+{}':
            esc = '\\'
        escapepat = escapepat + delimiter + esc + c
        delimiter = '|'
    to_escape = False
    if escapepat != '':
        to_escape = True
        escapepat = '(%s)' % escapepat
    separator = unicode(args.separator)

    def getinfo(file):
        try:
            return (file.info.bitrate / 1000, int(round(file.info.length)))
        except:
            return (0,0)

    for f in args.audiofile:
        path = unicode(os.path.realpath(f.decode('utf-8')))
        artist = title = album = year = wors = ""
        file = File(f)
        kbps, len = getinfo(file)
        try:
            if (type(file) == MP3):
                # add ID3 tag if it doesn't exist
                try:
                    file.add_tags()
                except:
                    pass
                # should we set the tag in anyway?
                if to_artist != '':
                    file.tags['TPE1'] = TPE1(encoding=3, text=to_artist)
                if to_album != '':
                    file.tags['TALB'] = TALB(encoding=3, text=to_album)
                if to_title != '':
                    file.tags['TIT2'] = TIT2(encoding=3, text=to_title)
                if to_wors != '':
                    file.tags['WORS'] = WORS(url=to_wors)
                if to_year != '':
                    file.tags['TDRL'] = TDRL(encoding=3, text=to_year)
                if to_cover != '':
                    #print('The image data is '+open(to_cover).read())
                    file.tags['APIC:'] = APIC(
                            encoding=3,
                            mime=to_cover_mime,
                            type=3,
                            data=open(to_cover).read()
                    )
                file.save()

                # process mp3 specific tag information
                artist = file.tags['TPE1'].text[0] if 'TPE1' in file.tags else ''
                album = file.tags['TALB'].text[0] if 'TALB' in file.tags else ''
                title = file.tags['TIT2'].text[0] if 'TIT2' in file.tags else ''
                wors = file.tags['WORS'].url if 'WORS' in file.tags else ''
#.........这里部分代码省略.........
开发者ID:lynnard,项目名称:mutagen-CLI,代码行数:103,代码来源:cli.py


注:本文中的mutagen.File.tags['TPE1']方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。