首页 > 编程语言 >使用Python清理重复音乐文件:一个简单的解决方案

使用Python清理重复音乐文件:一个简单的解决方案

时间:2024-03-30 23:33:57浏览次数:17  
标签:音乐文件 title Python 解决方案 length music file path

在日常生活中,我们经常会从各种渠道获取音乐资源,例如购买、下载或者从朋友那里借来。然而,有时候我们可能会发现自己的音乐库里存在着大量的重复音乐文件,这不仅浪费了存储空间,而且在听歌的时候也会带来不便。

针对这个问题,我编写了一个简单的Python程序来帮助清理重复的音乐文件。为什么选择Python呢?因为Python是一种功能强大且易于上手的编程语言,而且我的电脑中已经安装了Python环境,只需要一个VSCode就可以编写、调试和运行代码,非常符合我的需求。

首先,让我们来看一下这个程序的实现原理。程序主要分为两个部分:获取音乐文件的标题信息和遍历目录并删除重复的音乐文件。

from mutagen.mp3 import MP3
from mutagen.flac import FLAC
import os

def get_music_info(file_path):
    """获取音乐文件的标题信息"""
    # 获取文件扩展名
    _, ext = os.path.splitext(file_path)
    ext = ext.lower()

    if ext == ".mp3":
        try:
            audio = MP3(file_path)
            if "TIT2" in audio.tags:
                # 获取mp3文件的标题信息
                return audio.tags["TIT2"].text[0], audio.info.length
            else:
                return "Unknown Title", 0
        except Exception as e:
            print(f"Error reading mp3 file: {e}")
            return "Unknown Title", 0
    elif ext == ".flac":
        try:
            audio = FLAC(file_path)
            if "title" in audio:
                # 获取flac文件的标题信息
                return audio["title"][0], audio.info.length
            else:
                return "Unknown Title", 0
        except Exception as e:
            print(f"Error reading flac file: {e}")
            return "Unknown Title", 0
    else:
        print(f"Error reading flac file {ext}")
        return "Unknown Title", 0

def remove_duplicate_music(root_dir):
    """遍历目录并删除重复的音乐文件"""
    seen_titles = {}
    duplicate_music_count = 0
    for dirpath, _, filenames in os.walk(root_dir):
        for filename in filenames:
            file_path = os.path.join(dirpath, filename)
            if file_path.lower().endswith(('.mp3', '.flac')):
                music_title, music_info_length = get_music_info(file_path)
                if music_title in seen_titles:
                    # 比较两个文件信息的完整性,保留信息更全的文件
                    existing_title, existing_length, exsiting_path = seen_titles[music_title]
                    if music_info_length > existing_length:
                        # 删除已存在的文件
                        os.remove(exsiting_path)
                        print(f"已删除重复文件: {exsiting_path}")
                        seen_titles[music_title] = (music_title, music_info_length, file_path)
                    else:
                        # 删除当前的文件
                        os.remove(file_path)
                        duplicate_music_count += 1
                        print(f"已删除的重复文件: {file_path}")
                else:
                    seen_titles[music_title] = (music_title, music_info_length, file_path)
    print(f"总计重复的歌曲数目: {duplicate_music_count}")

root_directory = "E:\\BaiduNetdiskDownload\\Music\\001 每月抖音热歌"
remove_duplicate_music(root_directory)

以上是程序的代码实现,接下来我将简要解释一下其运行原理。

首先,程序通过使用mutagen库来解析音乐文件的元数据,从而获取音乐文件的标题信息和时长。然后,通过遍历指定目录下的所有音乐文件,将文件的标题信息和时长存储到一个字典中。在遍历的过程中,如果发现有重复的标题信息,就会进行比较,并保留信息更全的文件,删除其他重复文件。

最后,程序会输出总计删除的重复歌曲数目,以及每个被删除的重复文件的路径。

通过这个简单的Python程序,我成功地清理了我的音乐库中的重复音乐文件,节省了大量的存储空间,也让我在听歌的时候不再被重复的曲目打扰。希望这个小工具也能帮助到有类似需求的朋友们。

平时工作中主要使用c++、c#做开发,同样的功能使用c#引入TagLib来做也是非常方便:

using System;
using System.Collections.Generic;
using System.IO;
using TagLib;

class Program
{
    static (string, long) GetMusicInfo(string filePath)
    {
        string title = "Unknown Title";
        long length = 0;
        try
        {
            var file = TagLib.File.Create(filePath);
            title = file.Tag.Title;
            length = file.Properties.Duration.Ticks;
        }
        catch (Exception e)
        {
            Console.WriteLine($"Error reading file: {e.Message}");
        }
        return (title, length);
    }

    static void RemoveDuplicateMusic(string rootDir)
    {
        Dictionary<string, (string, long, string)> seenTitles = new Dictionary<string, (string, long, string)>();
        int duplicateMusicCount = 0;
        foreach (string filePath in Directory.GetFiles(rootDir, "*.*", SearchOption.AllDirectories))
        {
            string ext = Path.GetExtension(filePath).ToLower();
            if (ext == ".mp3" || ext == ".flac")
            {
                (string title, long length) = GetMusicInfo(filePath);
                if (seenTitles.ContainsKey(title))
                {
                    // 比较两个文件信息的完整性,保留信息更全的文件
                    var (existingTitle, existingLength, existingPath) = seenTitles[title];
                    if (length > existingLength)
                    {
                        // 删除已存在的文件
                        File.Delete(existingPath);
                        Console.WriteLine($"已删除重复文件: {existingPath}");
                        seenTitles[title] = (title, length, filePath);
                    }
                    else
                    {
                        // 删除当前的文件
                        File.Delete(filePath);
                        duplicateMusicCount++;
                        Console.WriteLine($"已删除的重复文件: {filePath}");
                    }
                }
                else
                {
                    seenTitles[title] = (title, length, filePath);
                }
            }
        }
        Console.WriteLine($"总计重复的歌曲数目: {duplicateMusicCount}");
    }

    static void Main(string[] args)
    {
        string rootDirectory = @"E:\BaiduNetdiskDownload\Music\001 每月抖音热歌";
        RemoveDuplicateMusic(rootDirectory);
    }
}

以上的c#代码片段的代码我没有调试, 不保证是否可用主要是记录一个思路, 另外涉及到整理资源包中的资源可能也会涉及到列出一些质量不太好的音乐出来,要验证音乐文件的质量,一种常见的方法是检查其音频属性,如比特率、采样率和编码方式。较低的比特率通常会导致音频质量较差。另外,还可以通过音频分析工具来检查音频的频谱和波形,以判断其质量。

以下是一种方法,使用Python和FFmpeg库来扫描音乐文件并获取其音频属性,以评估音乐质量:

import os
import subprocess
import json

def get_audio_properties(file_path):
    """获取音乐文件的属性"""
    cmd = ["ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", file_path]
    result = subprocess.run(cmd, capture_output = True)
    if result.returncode == 0:
        properties = json.loads(result.stdout)
        return properties.get("format", {})
    else:
        print(f"Error reading file: {file_path}")
        return {}
    
def scan_music_quality(root_dir, min_bitrate = 192000):
    """扫描音乐文件并列出质量差的文件"""
    low_quality_files = []
    for dirpath, _, filenames in os.walk(root_dir):
        for filename in filenames:
            file_path = os.path.join(dirpath, filename)
            if file_path.lower().endswith(('.mp3', '.flac')):
                properties = get_audio_properties(file_path)
                if properties:
                    bitrate = int(properties.get("bit_rate", 0))
                    if bitrate < min_bitrate:
                        low_quality_files.append((file_path, bitrate))
    return low_quality_files

#使用示例
root_directory = "E:\\BaiduNetdiskDownload\\Music\\001 每月抖音热歌"
low_quality_files = scan_music_quality(root_directory)
for file_path, bitrate in low_quality_files:
    print(f"Low quality file: {file_path}, Bitrate: {bitrate}")

标签:音乐文件,title,Python,解决方案,length,music,file,path
From: https://www.cnblogs.com/linxmouse/p/18106243

相关文章

  • Python NumPy库
    Python中列表(list),虽然可以当作数组使用,但是由于列表中的元素可以是任意对象,因此列表中所保存的是对象的指针,一个元素拥有一个指针和一个对象。对于数值运算来说,这种结构比较浪费。此外,Python的array模块,虽然可以直接保存数值,但是不支持多维,也没有各种函数。而NumPy库提供了......
  • [linux] ubuntu 下安装qtcreate遇到“无法加载Qt平台插件‘xcb’问题”解决方案
    [linux]ubuntu下安装qtcreate遇到“无法加载Qt平台插件‘xcb’问题”解决方案以下是遇到的三种报错情况From6.5.0,xcb-cursor0orlibxcb-cursor0isneededtoloadtheQtxcbplatformplugin.CouldnotloadtheQtplatformplugin“xcb”in“”eventhough......
  • Python之Opencv教程(2):图像边缘检测
    1、什么是边缘检测OpenCV中的边缘检测是一种常见的图像处理技术,用于检测图像中物体边缘的位置。常用的边缘检测算法包括Sobel算子、Scharr算子、Laplacian算子和Canny边缘检测算法等。下面将介绍使用OpenCV实现这些边缘检测算法的方法。2、边缘检测的作用边缘检测是图像......
  • 【测试开发学习历程】Python数据类型:字符串-str(下)
    目录5.5format()方法5.6count()方法5.7join()方法5.8replace()方法5.9split()方法5.10rstrip()/lstrip()/strip()方法5.11capitalize()方法5.12upper()5.13lower()5.14title()5.15endswith()方法5.16startswith()方法5.17以is开头的方法5转义字符......
  • 绚烂之境:Python Rich,让终端输出更炫酷!
    转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.top/c8c7bd95.html初识rich你好,我是测试蔡坨坨。在代码的世界里,每一行都是一个故事,每一个变量都是一个角色,而打印则是展示这些故事与角色的窗口。然而,这个窗口并非都是朴实无华的,有时候,我们需要一种更加「艺术」的方式来展......
  • 智慧工地整体解决方案(1)
    背景建筑行业是我国国民经济的重要物质生产部门和支柱产业之一,在改善居住条件、完善基础设施、吸纳劳动力就业、推动经济增长等方面发挥着重要作用。与此同时,建筑业也是一个安全事故多发的高危行业。近年来,在国家、各级地方政府主管部门和行业主体的高度关注和共同努力下,建筑......
  • 使用Jep在Java中调用Conda虚拟环境下的Python
    为了解决毕设中需要用到在Java中调用Python的问题,我在网上寻找对应的解决方案。似乎没有太好的解决方案:Jython至今仍是Python2,Py4J似乎也不再活跃更新。所幸我找到了Jep这一神器。正当我雀跃不已,却又发现了一些问题,在两个小时的艰难攻关之下,这些问题逐渐迎刃而解。问题一:无法找到......
  • Python面试题
    1、现有100万行的交易数据文件tansamt.txt,每行显示1个交易金额(首行无字段信息),样例如下。数据文件存放在服务器g:\data目录。203.495-5091800请通过Python脚本,把最大和最小的交易金额通过print命令打印出来:file_path=r'g:\data\tans_amt.txt'#打开文件并读取所有......
  • python-numpy-常用函数详解
    文章目录一、函数详解np.empty(num_points)np.zeros(shape,dtype=float,order='C')np.tile(A,reps)np.newaxisnp.stack(arrays,axis=0)np.roll(a,shift,axis=None)np.repeat(a,repeats,axis=None)arr.reshape(shape)arr.ravel()np.mean(a,axis=None,dtype=None......
  • 毕业设计:基于深度学习的SQL注入检测系统 信息安全 python
    目录前言课题背景和意义实现技术思路一、算法理论基础1.1 TextCNN模型1.2无监督数据增强二、 数据集2.1数据集2.2数据扩充三、实验及结果分析3.1 实验环境搭建3.2 模型训练最后前言  ......