« [PDF化]複数の画像ファイルをPDFにまとめる(解像度を72ppi化) | トップページ | [Music.app]選択曲の合計時間を求める »

[AVFoundation]オーディオファイルの時間の合計を求める

【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#  com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AVFoundation"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

#####ファイル選択ダイアログ
###ダイアログのデフォルト
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
tell application "Finder"
  activate
  
  set listChooseFileUTI to {"com.apple.m4a-audio", "public.mp3", "public.audio"}
  set strPromptText to "ファイルを選んでください" as text
  set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents)
end tell
set numTotalTime to 0 as integer
set strOutPutText to "\r" as text

repeat with objAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of objAliasFilePath as text
  set ocidFilePath to (refMe's NSString's stringWithString:strFilePath)
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
  set strFileName to ocidFilePathURL's lastPathComponent() as text
  ###################################
  #####アセット
  set ocidReadAsset to (refMe's AVURLAsset's URLAssetWithURL:ocidFilePathURL options:(missing value))
  ###################################
  #####タイトル
  set ocidCommonMetadata to ocidReadAsset's commonMetadata()
  set ocidTitleArray to (refMe's AVMetadataItem's metadataItemsFromArray:ocidCommonMetadata withKey:(refMe's AVMetadataCommonKeyTitle) keySpace:(refMe's AVMetadataKeySpaceCommon))
  try
    set ocidMetaTitle to (ocidTitleArray's objectAtIndex:0)'s value()
  on error
    set ocidMetaTitle to "不明:" & strFileName
  end try
  ###################################
  #####アーティスト
  set ocidArtistArray to (refMe's AVMetadataItem's metadataItemsFromArray:ocidCommonMetadata withKey:(refMe's AVMetadataCommonKeyArtist) keySpace:(refMe's AVMetadataKeySpaceCommon))
  try
    set ocidMetaArtist to (ocidArtistArray's objectAtIndex:0)'s value()
  on error
    set ocidMetaArtist to "不明:" & strFileName
  end try
  
  ###################################
  #####長さ
  set ocidReadAssetDuration to ocidReadAsset's duration()
  set numTime to refMe's CMTimeGetSeconds(ocidReadAssetDuration)
  
  set numMin to (round of (numTime / 60) rounding down)
  set numSec to (numTime - (numMin * 60)) as integer
  
  set strOutPutText to strOutPutText & ("" & numMin & "" & numSec & "秒:" & (ocidMetaTitle as text) & " - " & (ocidMetaArtist as text) & "\r")
  set numTotalTime to numTotalTime + numTime as number
end repeat

##############################
##表示
##############################
set numMin to (round of (numTotalTime / 60) rounding down)
set numSec to (numTotalTime - (numMin * 60)) as integer


log "合計時間は" & numTotalTime & "秒です"
log "合計時間は約;" & numMin & "" & numSec & ""
log strOutPutText

set strOutPutText to "合計時間は" & numTotalTime & "秒です\r" & "合計時間は約;" & numMin & "" & numSec & "\r" & strOutPutText as text


set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns" as alias
set strDefaultAnswer to "入力してください" as text
tell application "Finder"
  activate
  (display dialog "結果です" with title "結果です" default answer strOutPutText buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 30 without hidden answer)
end tell




|

« [PDF化]複数の画像ファイルをPDFにまとめる(解像度を72ppi化) | トップページ | [Music.app]選択曲の合計時間を求める »

AppleScript Music」カテゴリの記事