Sunday, November 29, 2020

#1 2020-11-20 12:04:18 pm

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 139

iTunes Script

I'm looking for some guidance on scripting iTunes.  I have playlists that change, and my car stereo is stubborn, so I'm trying to fix it.  This is what I want to do:

- My master playlist for the car gets broken down into 5 individual smart playlists (long tunes, band 1, band 2, band 3, other, etc.). 

- Tunes in these playlists have VBR (variable bit rates), and I need to export them to a designated Finder folder so I can convert them into a fixed bitrate of 320 kbps.  THAT PART I can do manually, the conversion.  I am trying to export those tunes that are VBR, without changing the master tune in iTunes. 

- Then I need to export the normal fixed bitrate tunes to those same destination folders, to make a complete playlist with subfolders.

I've had a look at the iTunes library (iTunes 12.8.2.3 for El Capitan) and I'm confused about how to get smart playlist names, how to identify a smart playlist, how to identify VBR files, and how to copy those files to a destination folder that I made. 

Any insight appreciated how or if I can get this accomplished.

Cheers


Filed under: iTunes, VBR

Offline

 

#2 2020-11-21 05:55:57 am

hubert0
Member
From:: East Anglia
Registered: 2020-11-20
Posts: 6

Re: iTunes Script

Hi,

First post here, hope this is OK.


daBee wrote:

how to get smart playlist names, how to identify a smart playlist, how to identify VBR files, and how to copy those files to a destination folder




I'm assuming that VBR files are those encoded using AAC, referred to in Finder as "Apple MPEG-4 audio". Please correct me if I'm wrong!

The following script was written for the newer Music.app but I think you should be able to substitute "iTunes" without any further changes.

It identifies your smart playlists, lists them by name, asks you to choose one and then uses Finder to duplicate all Apple MPEG-4 audio tracks from that playlist to a destination folder of your choice. It doesn't touch .mp3 files or "protected" files downloaded using an Apple Music subscription (as opposed to purchased from the Apple Music Store).

Applescript:

tell application "Music"
   activate
   set smartPlaylists to name of every user playlist whose smart is true -- make a list of every smart playlist
   set targetPlaylist to (choose from list smartPlaylists with prompt "Choose a playlist to duplicate:") -- gets name of chosen smart playlist
   if targetPlaylist is false then -- dialog was cancelled
       return
   else
       set targetPlaylist to targetPlaylist as string
   end if
   
   set destinationFolder to (choose folder with prompt "Choose a folder to duplicate AAC files:") -- specify destination for duplicates
   set trackLocations to location of every file track of playlist targetPlaylist -- make list of every track location in the chosen playlist
   
   
end tell

tell application "Finder"
   activate
   repeat with eachFile in trackLocations
       set trackKind to kind of eachFile
       if trackKind is "Apple MPEG-4 audio" then --it's presumably a VBR track
           try
               duplicate eachFile to destinationFolder
           on error errorNumber
               if errorNumber is -15267 then -- item already exists, skip
               end if
           end try
       end if
       
   end repeat
   display dialog "Finished duplicating AAC files" buttons {"Cool"} default button 1 giving up after 10
end tell

(You may want to mute your system volume before running it, as it makes a bit of a racket when duplicating smile ).

It needs more error checking to make it completely bombproof (it doesn't like playlists with no members, for example) but I hope it shows the basics of what you want to achieve.

Cheers,

H

Model: 2018 MacBook Pro running macOS Big Sur
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14


MacOS 11.0.1

Offline

 

#3 2020-11-21 07:34:31 pm

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 139

Re: iTunes Script

Hey thanks for the post.  Great script.  When someone else writes AS, it is so clear.  Heh.

I've used a test array for my source files, but the file type qualifier isn't taking.

The MP3 files are MPEG-1, Layer 3, both normal fixed bitrate and VBR. 

bit rate: 320 kbps (VBR)
bit rate: 320 kbps

That's the only difference I see.  So I'm not sure how I can trip that conditional into "VBR" or "normal". 

Any ideas?

Cheers

Offline

 

#4 2020-11-21 07:46:43 pm

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 139

Re: iTunes Script

Just got properties of two tracks and there is absolutely NO indication of any `VBR` on either. 

The `bit rate` shows up as the same 320 as the other.  This might be tougher than it looks.

Offline

 

#5 2020-11-21 08:51:57 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

Re: iTunes Script

You should get exif metadata of audio file for this task>

1) Download and install Command Line Interface (CLI) for MediaInfo from Here.
2) The script proposed by hubert0, with little tweaking from my side:

Applescript:


property mediaInfoPath : "usr/local/bin/mediainfo "

-- specify destination for duplicates
set destinationFolder to (choose folder with prompt "Choose a folder to duplicate VBR encoded files:")

tell application "Music"
   activate
   -- get the list of every smart playlist
   set smartPlaylists to name of every user playlist whose smart is true
   -- get name of chosen smart playlist
   set targetPlaylist to (choose from list smartPlaylists with prompt "Choose a playlist to duplicate:")
   if targetPlaylist is false then return -- dialog was cancelled
   set targetPlaylist to targetPlaylist as string
   -- get the list of every track location (aliases) in the chosen playlist
   set trackLocations to location of every file track of playlist targetPlaylist
end tell

repeat with eachFile in trackLocations
   set audioFile to quoted form of (POSIX path of eachFile)
   set BitRateMode to do shell script (mediaInfoPath & "--Inform=\"Audio;%BitRate_Mode%\" " & audioFile)
   if BitRateMode is "VBR" then try -- Maybe, item already exists, try block is need
       tell application "Finder" to duplicate eachFile to destinationFolder
   end try
end repeat

display dialog "Finished duplicating VBR-encoded files" buttons {"Cool"} default button 1 giving up after 10

Last edited by KniazidisR (2020-11-21 09:43:10 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#6 2020-11-22 09:32:23 am

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 981

Re: iTunes Script

Hi. I see a couple ways to workaround the issue without resorting to downloading add-ons.

#1) There should be unutilized fields in iTunes' file representations; e.g.comment, volume adjustment, season number, etc. View your library by song, then sort by bit rate; the VBR files will be contiguous, and you can select and edit them as a group (Command+I) to insert a VBR/CBR identifier that a script can target.

#2) [The initial code for this method was redacted. There appears to be a misreporting in iTunes' files' duration and further testing proved erroneous.]

Last edited by Marc Anthony (2020-11-22 09:27:04 pm)

Offline

 

#7 2020-11-22 10:38:41 am

hubert0
Member
From:: East Anglia
Registered: 2020-11-20
Posts: 6

Re: iTunes Script

KniazidisR wrote:

Download and installCommand Line Interface (CLI) for MediaInfo



Nice! A useful addition to the toolkit cool


MacOS 11.0.1

Offline

 

#8 2020-11-22 11:40:59 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

Re: iTunes Script

If you use mediainfo you could filter the result with grep.

ex.

Applescript:

set pathToMusicFile to POSIX path of (choose file) as text
do shell script "mediainfo " & quoted form of pathToMusicFile & "| grep 'Codec\\|Overall\\|Bit'"

If you like to have a property string with multiply search string separated with comma.

Applescript:

property theString : "Codec,Overall,Bit"

set grepString to my replaceString:theString withString:"\\|"

set pathToMusicFile to POSIX path of (choose file) as text
do shell script "mediainfo " & quoted form of pathToMusicFile & "| grep '" & grepString & "'"

on replaceString:_oldString withString:_newString
   set AppleScript's text item delimiters to {","}
   set theString to text items of _oldString
   set AppleScript's text item delimiters to {_newString}
   set theString to theString as text
   set AppleScript's text item delimiters to {""}
   return theString
end replaceString:withString:

Last edited by Fredrik71 (2020-11-22 12:33:51 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#9 2020-11-22 02:00:50 pm

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 139

Re: iTunes Script

OK, great solution.  I'm finding that the iTunes bitrates are mis-represented.  Get Info on a tune updates the bitrate.  Been chasing that stuff.

But further testing means I have the proper files where they need to be.  Just so that people know, a VBR file can also show 320 kbps.  So the analytical approach might be false.  Also, the hit list is minimal, so not achieving anything.  Conversion in the Finder by a third app works just fine. 

Onto the next question.

Thanks for all the help!

Offline

 

#10 2020-11-23 12:08:36 am

technomorph
Member
Registered: 2017-12-14
Posts: 173

Re: iTunes Script

What is the reason for the conversion?  Will your car player not play them?

Is there any reason for you to NOT HAVE TO convert the files that are already CBR?
(since your exporting files and keeping them completely separate from your iTunes library files)

IE Export all the playlist track files to a folder, along with a M3U playlist file.
Convert ALL the tracks as your would if the were VBR. (if the tracks were already CBR
your converter should not really do anything)
This way you don't have to worry about your "filtering out the VBR tracks"....

I highly recommend getting your Library Maintenance done ahead of all these tasks.
Fix the VBR files and convert them once and for all.  Then done.
(Also convert your files before you import them).


Here's some handy iTunes Script Menu scripts from Doug's Scripts

- FULL APP TO DO WHAT YOUR LOOKING TO DO
https://dougscripts.com/apps/m3unifyapp.php

- CONVERT AND REPLACE:
https://dougscripts.com/itunes/scripts/ … andreplace

Offline

 

#11 2020-11-23 06:04:38 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

Re: iTunes Script

My approach for my conversions from flac to (I do this for my Pioneer Car Stereo library to output
to lossless wav) to store them on 500 GB SSD... and it only take a few minutes. (very fast)

When Steve Jobs said: Thousands songs in the pocket, I could have 500 songs in
lossless wav on SDHC (32GB) and that is enough to me.

I use xld
https://tmkk.undo.jp/xld/index_e.html

If you have arm64 here is a beta.
https://sourceforge.net/p/xld/tickets/517/

I'm sure you could do the same...
1. Setup the output format.
2. Setup other options
3. Import track

You will have a subfolder with new tracks.


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#12 2020-11-24 05:57:42 pm

technomorph
Member
Registered: 2017-12-14
Posts: 173

Re: iTunes Script

Fredrik71 wrote:

My approach for my conversions from flac to (I do this for my Pioneer Car Stereo library to output
to lossless wav) to store them on 500 GB SSD... and it only take a few minutes. (very fast)

When Steve Jobs said: Thousands songs in the pocket, I could have 500 songs in
lossless wav on SDHC (32GB) and that is enough to me.



He’s using iTunes and iTunes doesn’t support FLAC.
It will support WAV but they don’t support ID3 rah embedding.

As a DJ I get FLACs and convert them to 520 m4a files
Using dBAmp Power Convertor.  It’s the only converter
I’ve found that will convert to a 520 m4a all others (including XLD
Which is an awesome program) will only convert to a max 320 m4a

Offline

 

#13 2020-11-24 06:11:14 pm

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 139

Re: iTunes Script

Fredrik71 wrote:

My approach for my conversions from flac to (I do this for my Pioneer Car Stereo library to output
to lossless wav) to store them on 500 GB SSD... and it only take a few minutes. (very fast)

When Steve Jobs said: Thousands songs in the pocket, I could have 500 songs in
lossless wav on SDHC (32GB) and that is enough to me.

I use xld
https://tmkk.undo.jp/xld/index_e.html

If you have arm64 here is a beta.
https://sourceforge.net/p/xld/tickets/517/

I'm sure you could do the same...
1. Setup the output format.
2. Setup other options
3. Import track

You will have a subfolder with new tracks.



I have VBRs in iTunes from various sources.  They're stored on a USB3 RAID, and my first step is an export to VBR and normal parent directories, based on the internal playlists.  So once exported, I use MediaHuman Converter for the VBR directories, which doesn't take long.  I then consolidate into the normal directory per child playlist, and I'm ready to copy to a 32 GB thumb drive as is

Boom.  Lovely solution.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)