Fileprocessing Methods

Fileprocessing Class

class musicdb.lib.fileprocessing.Fileprocessing(root='/')[source]

This class extends the interface to the musicdb.lib.filesystem.Filesystem class. It provides file processing methods. These methods may execute other linux shell commands.

digraph hierarchy { size="5,5" node[shape=record,style=filled,fillcolor=gray95] edge[dir=back, arrowtail=empty] filesystem [label = "{Filesystem||}"] fileprocessing [label = "{Fileprocessing||}"] filesystem -> fileprocessing }

Whenever I write about root director the path set in this class as root is meant. Otherwise I would call it system root directory.

All paths of the methods provided by this class can be absolute or relative. The paths of methods from musicdb.lib.filesystem.Filesystem must be in the format as defined for that class.

Parameters

root (str) – Path to the internal used root directory. It is allowd to start with “./”.

Raises

ValueError – if the root path does not start with "/" or does not exist

Checksum(path, algorithm='sha256')[source]

This method calculates the checksum of a file and returns it hexadecimal encoded as a string. If the file does not exists, None gets returned.

The default algorithm is SHA-256

Parameters
  • path (str) – Path to the file from that the checksum shall be calculated

  • algorithm (str) – Checksum algorithm: "sha256", "sha1"

Returns

Checksum as string, or None if the file does not exist.

Raises

ValueError – For unknown algorithm argument.

Example

checksum = fs.Checksum(song["path"])
print("Checksum: %s" % (checksum))
ConvertToMP3(srcpath, dstpath)[source]

This method converts any audio file format to an mp3 file using ffmpeg.

The encoder is libmp3lame and the bitrate is esoteric 320kbit/s. If the destination file exists, it will be overwritten.

Source and destination path must be different.

This method corresponds to the following command line:

ffmpeg -v quiet -y -i $abssrcpath -acodec libmp3lame -ab 320k $absdstpath < /dev/null > /dev/null 2>&1

Warning

Call os.sync if the generated file will be further processed. In the past, there were lots of trouble with “incomplete” files.

Parameters
  • srcpath (str) – source path of a song file with any encoding

  • dstpath (str) – destination path of the new generated song file with mp3 encoding.

Returns

True on success, otherwise False

Raises

ValueError – When srcpath and dstpath address the same file

ExistsProgram(programname: str) bool[source]

This method checks if a program exists.

Example

if fp.ExistsProgram("ffmpeg") == False:
    print("ffmpeg is not installed!");
Parameters

programname (str) – Name of the program to check for

Returns

True if the program exists, otherwise False

OptimizeM4ATags(mdbsong, mdbalbum, mdbartist, srcpath, dstpath)[source]

This method fixes the Tags of an m4a file. The data for the Tags come from the MusicDB Database. For writing the new tags, ffmpeg is used.

Source and destination path must be different.

The call of ffmpeg corresponds to the following command line that got condensed to just setting the songname.

ffmpeg -v quiet -y -i $abssrcpath -vn -acodec copy -metadata title="Name of the Song" $absdstpath < /dev/null > /dev/null 2>&1

The following tags will be set:

  • Song name

  • Album name

  • Artist name

  • Release date

  • Track number

  • CD number

Warning

Other tags could be removed - I don’t care about them. If ffmpeg also doesn’t care, they are lost. This will definitely happen with iTunes related tags like the Apple ID and purchase-date.

Artwork will be removed, too :( - This is considered as a bug. I just have no solution to fix it right now.

The following code should preserve the artwork but throws an error: ffmpeg -y -i src.m4a -map 0:0 -map 0:1 -vcodec copy -acodec copy -metadata "title=Title" dst.m4a

Source and destination file must be different. The source file must have the file extension .m4a or .mp4.

Parameters
  • mdbsong – The corresponding song-object from the MusicDB Database

  • mdbalbum – The corresponding album-object from the MusicDB Database

  • mdbartist – The corresponding artist-object from the MusicDB Database

  • srcpath (str) – source path of the m4a-file (must be m4a!)

  • dstpath (str) – destination path for the new m4a-file. (Must be different from the source path)

Returns

True on success, otherwise False

OptimizeMP3Tags(mdbsong, mdbalbum, mdbartist, srcpath, dstpath, absartworkpath=None, forceID3v230=False)[source]

This method fixed the ID3 tags of an mp3 file. For writing the new ID3 tags, id3edit is used.

Source and destination path can be the same. The artwork path must be absolute!

The call of id3edit corresponds to the following command line, reduced to just setting the songname.

id3edit --clear --create --set-name "Name of the Song" --outfile $absdstpath $abssrcpath < /dev/null > /dev/null 2>&1

The following tags will be set. All other will be removed.

  • Song name

  • Album name

  • Artist name

  • Release date

  • Track number

  • CD number

  • Artwork

Warning

All other tags will not be copied to the new file. The id3edit call creates a totaly new ID3v2 header for the mp3 file.

Parameters
  • mdbsong – The corresponding song-object from the MusicDB Database

  • mdbalbum – The corresponding album-object from the MusicDB Database

  • mdbartist – The corresponding artist-object from the MusicDB Database

  • srcpath (str) – absolute source path of the mp3-file (must be mp3!)

  • dstpath (str) – absolute destination path for the new mp3-file. (Can be the same as the source path)

  • absartworkpath (str) – if not None the album artwork will be stored inside the ID3 tags. Otherwise it will be removed.

  • forceID3v230 (bool) – use old ID3v2.3.0 tags instead of modern 2.4.0. Some player don’t like a version number other than 2.3.0.

Returns

True on success, otherwise False

Raises

ValueError – When the source or destination path is not an mp3 file.