Editing metadata using Terminal

How can I edit the metadata of an MP3 file based on the name?


I already have a script that renames the file correctly (see below). In the process, it uses variables for the elements. I would like to then use those variable to insert the data into the metadata.


#! /bin/zsh -
set -o extendedglob
pattern='(#b)*_ \((<0-999>)\)(* )—( * )(<1-12>)_(<1-31>)_(<1900-2100>)(.mp3)'
ret=0
for file {
  if [[ $file = $~pattern ]] {
    argv=( "$match[@]" )
    mv -i -- $file "${(l[3][0])1} -$2-$3- $6-${(l[2][0])5}-${(l[2][0])4}-0900$7" || ret=$?
  }
}
exit $ret


Using this script, this file:

Public Talks_ (9) Walking With God Brings Blessings Now and Forever — Chris Ruscher 10_28_2023.mp3

is renamed to:

009 - Walking With God Brings Blessings Now and Forever - Chris Ruscher - 2023-28-10-0900.mp3


The format is: track# - title - artist - date.mp3

Showing the variables: $1 - $2 - $3 - $6-$5-$4-0900$7


So I would like to then insert those values into the metadata seen below.

a. track ($1) >> kMDItemAudioTrackNumber

b. title ($2) >> kMDItemTitle     

c. artist ($3) >> kMDItemAuthors

d. timestamp ($6${(l[2][0])5}${(l[2][0])4}0900) >> kMDItemContentCreationDate


The script would also modify the file creation date to kMDItemContentCreationDate. I tried adding the touch -t command to the loop but it didn't work for this last step (see 2 new lines with > below)


#! /bin/zsh -
set -o extendedglob
pattern='(#b)*_ \((<0-999>)\)(* )—( * )(<1-12>)_(<1-31>)_(<1900-2100>)(.mp3)'
ret=0
for file {
  if [[ $file = $~pattern ]] {
    argv=( "$match[@]" )
>   fecha=( "$6${(l[2][0])5}${(l[2][0])4}0900$7" )
>   touch -t $(date -j -f "%Y%m%d%H%M" $fecha +%Y%m%d%H%M) $file
    mv -i -- $file "${(l[3][0])1} -$2-$3- $6-${(l[2][0])5}-${(l[2][0])4}-0900$7" || ret=$?
  }
}
exit $ret


Any ideas? I am new to scripting so I have cobbled together what I have found on the forum. Your insight is much appreciated.


MacBook Air (M1, 2020)

Posted on Nov 20, 2023 5:51 AM

Reply

There are no replies.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Editing metadata using Terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.