首页 > 解决方案 > 使用元数据在计算机上搜索 mp3/mp4 - tinytag python

问题描述

问题

我正在尝试使用 os.walk 在我的计算机上搜索与标签元数据匹配的 mp3 和 mp4。我希望能够搜索马里奥,并根据歌曲名称或艺术家、专辑出现歌曲或视频列表。

我正在使用 Tinytag 从文件中获取元数据,但我不知道如何应用 OS.walk 和 Tinytag 根据歌曲名称、艺术家或专辑搜索我的计算机。

TinyTag 文档:https ://pypi.org/project/tinytag/

tinytag 工作原理的示例代码:

from tinytag import TinyTag
tag = TinyTag.get(r"\\Vgmstation\e\Gamecube\F-Zero GX\8 Guitars (Sand Ocean).mp4")
print('This track is by %s.' % tag.artist)
print('It is %f seconds long.' % tag.duration)

结果:

This track is by Hidenor Shoji.
It is 154.087267 seconds long.

我的 os.walk 搜索目前是如何工作的。现在我将其设置为仅查找前五个结果:

import os
import subprocess

def playmusic(name):
    count = 0
    extf = ['$RECYCLE.BIN','System Volume Information']
    for root, dirs, files in os.walk('E:\\', followlinks=True):
        dirs[:] = [d for d in dirs if d not in extf]
        for file in files:
            if name in file and file.endswith(".mp4"):
                music=str(os.path.join(file))
                musiconly= os.path.splitext(music)[0]
                print(musiconly)
                count = count + 1 
                if count == 5:
                    break
        if count >= 5:
            break
    print("Finish")
    input()
try:
    s=raw_input("name: ")
    playmusic(s)
except Exception as e:
    print(e)
    print("Error")

结果:

name: te
00044converted
crash crate jump
footy_MAGIX AVC-AAC MP4_Internet HD 1080p 29.97 fps (Intel QSV) final[0]
footy_MAGIX AVC-AAC MP4_Internet HD 1080p 29.97 fps (Intel QSV) final[1]
Untitled3_MAGIX AVC-AAC MP4_Internet HD 1080p 29.97 fps (Intel QSV) final[0]
Finish

如果我能找到一种将两者结合起来的方法,我就会拥有我需要的东西。我尝试做一些 if 语句,例如:if TinyTag.get(name)或者if TinyTag.get(music)但我不确定在哪里应用它来提取以搜索查询开头或匹配的结果的特定结果。

任何想法或建议将不胜感激。

标签: pythonfor-loopif-statementmetadataos.walk

解决方案


推荐阅读