实用脚本能自动提取音乐专辑封面吗?

wen 实用脚本 3

本文目录导读:

实用脚本能自动提取音乐专辑封面吗?

  1. Python脚本方案
  2. 命令行工具
  3. 专用软件
  4. 一键脚本(跨平台)
  5. 在线API方案
  6. 推荐方案

是的,有许多实用脚本和工具可以自动提取音乐专辑封面,以下是几种常见方法:

Python脚本方案

使用 mutagen 库

from mutagen.flac import FLAC
from mutagen.mp4 import MP4
from mutagen.id3 import ID3
import os
def extract_cover(music_file):
    """从音乐文件中提取封面"""
    try:
        if music_file.endswith('.flac'):
            audio = FLAC(music_file)
            if audio.pictures:
                with open('cover.jpg', 'wb') as f:
                    f.write(audio.pictures[0].data)
        elif music_file.endswith('.mp3'):
            audio = ID3(music_file)
            for tag in audio.getall('APIC'):
                with open('cover.jpg', 'wb') as f:
                    f.write(tag.data)
        elif music_file.endswith('.m4a'):
            audio = MP4(music_file)
            if 'covr' in audio:
                cover_data = audio['covr'][0]
                with open('cover.jpg', 'wb') as f:
                    f.write(cover_data)
        print(f"封面已提取到 cover.jpg")
    except Exception as e:
        print(f"提取失败: {e}")
# 使用示例
extract_cover('song.flac')

批量处理脚本

import os
from pathlib import Path
def batch_extract(folder_path):
    """批量提取文件夹内所有音乐文件的封面"""
    for file_path in Path(folder_path).rglob('*'):
        if file_path.suffix.lower() in ['.mp3', '.flac', '.m4a', '.wma']:
            output_name = file_path.stem + '_cover.jpg'
            extract_cover(str(file_path))
            os.rename('cover.jpg', output_name)

命令行工具

ffmpeg(最通用)

# 从MP3提取封面
ffmpeg -i input.mp3 -an -vcodec copy cover.jpg
# 从FLAC提取封面
ffmpeg -i input.flac -an -vcodec copy cover.jpg
# 批量提取
for file in *.mp3; do
    ffmpeg -i "$file" -an -vcodec copy "${file%.*}_cover.jpg"
done

kid3-cli(KDE音乐标签工具)

# 安装
sudo apt install kid3-cli  # Debian/Ubuntu
# 提取封面
kid3-cli -c "export cover cover.jpg" song.mp3
# 批量提取
kid3-cli -c "select *.mp3" -c "export cover cover.jpg"

专用软件

MusicBrainz Picard

# 安装
pip install picard
# 使用命令行提取封面
picard -c "cover_art_export_dir=/path/to/output" file.mp3

beets(音乐管理工具)

# 在config.yaml中配置
import:
    copy_album_art: yes
    write_album_art: yes
    album_art_maxwidth: 500
# 命令
beet extract -a  # 提取所有专辑封面

一键脚本(跨平台)

#!/bin/bash
# extract_cover.sh - 通用封面提取脚本
extract_cover() {
    local file="$1"
    local output="${file%.*}_cover.jpg"
    # 尝试多种方法
    ffmpeg -i "$file" -an -vcodec copy "$output" 2>/dev/null || \
    kid3-cli -c "export cover $output" "$file" 2>/dev/null || \
    python3 -c "
from mutagen import File
audio = File('$file')
for tag in (audio.tags or []):
    if hasattr(tag, 'data'):
        with open('$output', 'wb') as f:
            f.write(tag.data)
" 2>/dev/null || echo "无法提取封面: $file"
}
# 批量处理
for f in "$@"; do
    extract_cover "$f"
done

在线API方案

如需从网络自动获取封面,可以结合:

  • Last.fm API:根据歌曲信息搜索封面
  • Discogs API:通过音乐数据库查找封面
  • Spotify API:获取官方专辑封面
import requests
def fetch_cover_from_api(artist, album):
    """从Last.fm获取封面"""
    API_KEY = "your_api_key"
    url = f"http://ws.audioscrobbler.com/2.0/"
    params = {
        'method': 'album.getinfo',
        'api_key': API_KEY,
        'artist': artist,
        'album': album,
        'format': 'json'
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data['album']['image'][3]['#text']  # 最大尺寸

推荐方案

  • 简单需求:使用 ffmpeg 一行命令
  • 批量处理:使用 Python 脚本 + mutagen
  • 自动化管理:使用 beets 音乐管理工具
  • 网络获取:结合音乐库 API

我可以帮你定制特定需求的脚本,需要什么格式或功能的封面提取工具?

抱歉,评论功能暂时关闭!