Esempio n. 1
0
    def setTag(self, tag, srcfile, coverpath=None):
        path = pathHelper.getDirName(srcfile)
        name = pathHelper.getFileNameWithoutExtension(srcfile)
        ext = pathHelper.getFileExtension(srcfile)
        oext = ext

        if 'm4a' in ext or 'mp4' in ext:
            oext = '.mp3'
        if 'mp3' not in oext:
            coverpath = None
        tmpfile = path + '/' + 'TMP' + name + oext

        try:
            data = AudioSegment.from_file(srcfile, format=ext[1:])
            check = data.export(tmpfile,
                                format=oext[1:],
                                tags=tag,
                                cover=coverpath)
            check.close()
        except Exception as e:
            pathHelper.remove(tmpfile)
            return

        if fileHelper.getFileSize(tmpfile) > 0:
            pathHelper.remove(srcfile)
            os.rename(tmpfile, path + '/' + name + oext)
        else:
            pathHelper.remove(tmpfile)
Esempio n. 2
0
 def setTrackMetadata(self, track_info, file_path, album_info, index):
     path = pathHelper.getDirName(file_path)
     name = pathHelper.getFileNameWithoutExtension(file_path)
     exte = pathHelper.getFileExtension(file_path)
     tmpfile = path + '/' + self.tmpfileFlag + name + exte
     try:
         tag = {
             'Artist': track_info['artist']['name'],
             'Album': track_info['album']['title'],
             'Title': track_info['title'],
             'CopyRight': track_info['copyright'],
             'Track': track_info['trackNumber']
         }
         if index is not None:
             tag['Track'] = str(index)
         if album_info is not None:
             tag['Date'] = album_info['releaseDate']
             tag['Year'] = album_info['releaseDate'].split('-')[0]
         # tmp file
         pathHelper.copyFile(file_path, tmpfile)
         # set metadata
         ext = os.path.splitext(tmpfile)[1][1:]
         data = AudioSegment.from_file(tmpfile, ext)
         check = data.export(tmpfile, format=ext, tags=tag)
         # check file size
         if fileHelper.getFileSize(tmpfile) > 0:
             pathHelper.remove(file_path)
             pathHelper.copyFile(tmpfile, file_path)
     except:
         pass
     if os.access(tmpfile, 0):
         pathHelper.remove(tmpfile)