renaming .flac files from tags

Discuss and share scripts and script files...
Post Reply
Ysl
Posts: 165
Joined: 03 Jan 2007 22:22

renaming .flac files from tags

Post by Ysl »

Hi

I looking for a way (UDC script) to rename .flac files with "artist - trackname" but I do not find a way to read the tag of the flac file.

AI tell me to use C:\Program Files (x86)\K-Lite Codec Pack\Tools\mediainfo.exe but all I get is the Mediainfo windows opening several times...

I'm sure a human can do best than AI on this case ;)

Thanks for your help !

highend
Posts: 14956
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: renaming .flac files from tags

Post by highend »

$artist = property("#tag.artist", $item);
$title = property("#tag.title", $item);

There is no need for mediainfo just to read those flac tags...
One of my scripts helped you out? Please donate via Paypal

Ysl
Posts: 165
Joined: 03 Jan 2007 22:22

Re: renaming .flac files from tags

Post by Ysl »

Yes indeed it prove human is still better even for those tasks !
Thank you, it work now :)

Ysl
Posts: 165
Joined: 03 Jan 2007 22:22

Re: renaming .flac files from tags

Post by Ysl »

It's me again :)

Could someone adapt this renaming .flac files script to .m4a files (artist - tracktitle) ?

Thanks :)

status "Début du script...", "0000FF", "ready";

$items = get("SelectedItemsPathNames", <crlf>);
if ($items == "") {
status "Aucun fichier sélectionné !", "FF0000", "stop";
end true;
}

$renamed = 0;
$total = 0;
$ignored = 0;

foreach($item, $items, <crlf>) {
if (exists($item) != 1) { continue; }

$ext = gpc($item, "ext");
if ($ext != "flac" && $ext != "FLAC") { continue; }

$total++;
$basename = gpc($item, "base");
$folder = gpc($item, "path");

// --- LIRE LES TAGS AVEC property("#tag.") ---
$title = property("#tag.title", $item);
$artist = property("#tag.artist", $item);

// --- FALLBACK SI LES TAGS SONT VIDES ---
if ($title == "") {
if (regexmatches($basename, "^\d+")) {
$title = regexreplace($basename, "^\d+\s*[-_\.]\s*", "");
if ($title == $basename) {
$title = regexreplace($basename, "^\d+\s+", "");
}
} else {
$title = $basename;
}
$title = regexreplace($title, "_", " ");
}

if ($artist == "") {
$parent = gpc($item, "path");
$parentname = gpc($parent, "base");
if (strpos($parentname, " - ") > 0) {
$artist = gettoken($parentname, 1, " - ");
} else {
$artist = "Artiste Inconnu";
}
}

// --- NETTOYER LE TITRE ---
$title = regexreplace($title, '^' . regexreplace($artist, '([.*+?^=!:${}()|\[\]\/\\])', '\\$1') . ' - ', '');

// --- CONSTRUIRE LE NOUVEAU NOM ---
$newname = $artist . " - " . $title . ".flac";
$newname = regexreplace($newname, '[<>:"/\\|?*]', '');

// --- VÉRIFIER SI LE NOM EST DÉJÀ CORRECT ---
$basename_clean = regexreplace($basename, '\.flac$', '');
$newname_clean = regexreplace($newname, '\.flac$', '');

if ($basename_clean == $newname_clean) {
$ignored++;
continue;
}

// --- RENOMMER ---
if ($newname != $basename . ".flac") {
$fullnewname = $folder . "\" . $newname;
if (exists($fullnewname) == 1) {
status "⚠ Existe déjà: " . $newname, "FFA500", "ready";
continue;
}

$result = renameitem($newname, $item);
if ($result == 1) {
$renamed++;
status "✓ " . $renamed . "/" . $total . " - " . $newname, "008000", "ready";
} else {
status "✗ Échec: " . $basename . ".flac", "FF0000", "stop";
}
}
}

status "✅ " . $renamed . " renommés, " . $ignored . " ignorés sur " . $total . " fichiers.", "008000", "ready";

highend
Posts: 14956
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: renaming .flac files from tags

Post by highend »

for m4a files you need mediainfo (the CLI version!) again...

and you either have to call it twice for each file (artist / title) or use a template file...
One of my scripts helped you out? Please donate via Paypal

Post Reply