Archive for the 'AVAsset' Category

2016/10/25 指定ムービーを指定形式に変換する v2

指定のムービーファイルを指定形式に変換するAppleScriptの改良版です。

一晩たったら、旧石器時代から現代までタイムスリップしたぐらいの改良が加わっています(汗)。野蛮な手段でファイル拡張子からUTIを求めていたのが、フレームワーク呼び出し一発で、、、処理速度も大幅に向上しています。

AppleScript名:指定ムービーを指定形式に変換する v2
– Created 2016-10-24 by Shane Stanley
– Modified 2016-10-24 by Takaaki Naganoya
– Modified 2016-10-25 by Shane Stanley
use AppleScript version “2.4″
use framework “Foundation”
use framework “AVFoundation”
use scripting additions
use BridgePlus : script “BridgePlus” –Version 1.3.2以降

load framework
–http://piyocast.com/as/archives/4288

set posixPath to POSIX path of (choose file with prompt “Choose a Movie file:”)
my convertMovieAt:posixPath ToType:“AVAssetExportPresetAppleM4A” withExtension:“m4a” deleteOriginal:false

–指定のムービーを、指定の書き出しプリセットで、指定のファイル形式で、オリジナルファイルを削除するかどうか指定しつつ書き出し
on convertMovieAt:(posixPath as string) ToType:(assetTypeStr as string) withExtension:(aExt as string) deleteOriginal:(deleteFlag as boolean)
  set theURL to current application’s |NSURL|’s fileURLWithPath:posixPath
  
  
set aPreset to current application’s NSString’s stringWithString:assetTypeStr
  
  
– set destination to use same path, different extension
  
set destURL to theURL’s URLByDeletingPathExtension()’s URLByAppendingPathExtension:aExt
  
set theAsset to current application’s AVAsset’s assetWithURL:theURL
  
  
– check asset can be converted
  
set allowedPresets to current application’s AVAssetExportSession’s exportPresetsCompatibleWithAsset:theAsset
  
if (allowedPresets’s containsObject:aPreset) as boolean is false then
    error “Can’t export this file as an .” & aExt & ” file.”
  end if
  
  
– set up export session
  
set fileTypeUTIstr to retFileFormatUTI(aExt) of me –ファイル拡張子からFile Type UTIを求める
  
set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:aPreset
  
theSession’s setOutputFileType:fileTypeUTIstr
  
theSession’s setOutputURL:destURL
  
  
– begin export and poll for completion
  
theSession’s exportAsynchronouslyWithCompletionHandler:(missing value)
  
repeat
    set theStatus to theSession’s status() as integer
    
if theStatus < 3 then
      delay 0.2
    else
      exit repeat
    end if
  end repeat
  
  
– throw error if it failed
  
if theStatus = (current application’s AVAssetExportSessionStatusFailed) as integer then
    error (theSession’s |error|()’s localizedDescription() as text)
  end if
  
  
– delete original if required
  
if deleteFlag then
    current application’s NSFileManager’s defaultManager()’s removeItemAtURL:theURL |error|:(missing value)
  end if
end convertMovieAt:ToType:withExtension:deleteOriginal:

on retFileFormatUTI(aExt as string)
  return (current application’s SMSForder’s UTIForExtension:aExt)
end retFileFormatUTI

★Click Here to Open This Script 

2016/10/24 指定ムービーを指定形式に変換する

指定のムービーファイルを指定形式に変換するAppleScriptです。

オリジナルはShane StanleyがAppleScript Users-MLに投稿したものですが、変換(エクスポート)形式が固定で指定されていました(ムービーからオーディオ部のみ書き出し)。

たいへんに有用性が高いものの、エクスポート形式固定のままでは使い勝手がいまひとつだったので、形式を可変指定できるように改修してみました。

エクスポート形式で指定可能なものは、このように一覧を取得できます。この中から選択してください。

本Scriptをそのまま実行すると、指定のムービーファイルから音声部分のみ、同一ファイル名で拡張子を「m4a」にした別ファイルをオリジナルのファイルと同一フォルダ内に書き出します。オリジナルのファイルは消去せずにそのまま残す設定です。

AppleScript名:指定ムービーを指定形式に変換する
– Created 2016-10-24 by Shane Stanley
– Modified 2016-10-24 by Takaaki Naganoya
use AppleScript version “2.4″
use framework “Foundation”
use framework “AVFoundation”
use scripting additions
–http://piyocast.com/as/archives/4286

set posixPath to POSIX path of (choose file with prompt “Choose a Movie file:”)
my convertMovieAt:posixPath ToType:“AVAssetExportPresetAppleM4A” withExtension:“m4a” deleteOriginal:false

–指定のムービーを、指定の書き出しプリセットで、指定のファイル形式で、オリジナルファイルを削除するかどうか指定しつつ書き出し
on convertMovieAt:(posixPath as string) ToType:(assetTypeStr as string) withExtension:(aExt as string) deleteOriginal:(deleteFlag as boolean)
  set theURL to current application’s |NSURL|’s fileURLWithPath:posixPath
  
  
set aPreset to current application’s NSString’s stringWithString:assetTypeStr
  
  
– set destination to use same path, different extension
  
set destURL to theURL’s URLByDeletingPathExtension()’s URLByAppendingPathExtension:aExt
  
set theAsset to current application’s AVAsset’s assetWithURL:theURL
  
  
– check asset can be converted
  
set allowedPresets to current application’s AVAssetExportSession’s exportPresetsCompatibleWithAsset:theAsset
  
if (allowedPresets’s containsObject:aPreset) as boolean is false then
    error “Can’t export this file as an .” & aExt & ” file.”
  end if
  
  
– set up export session
  
set fileTypeUTIstr to retFileFormatUTI(aExt) of me –ファイル拡張子からFile Type UTIを求める
  
set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:aPreset
  
theSession’s setOutputFileType:fileTypeUTIstr
  
theSession’s setOutputURL:destURL
  
  
– begin export and poll for completion
  
theSession’s exportAsynchronouslyWithCompletionHandler:(missing value)
  
repeat
    set theStatus to theSession’s status() as integer
    
if theStatus < 3 then
      delay 0.2
    else
      exit repeat
    end if
  end repeat
  
  
– throw error if it failed
  
if theStatus = (current application’s AVAssetExportSessionStatusFailed) as integer then
    error (theSession’s |error|()’s localizedDescription() as text)
  end if
  
  
– delete original if required
  
if deleteFlag then
    current application’s NSFileManager’s defaultManager()’s removeItemAtURL:theURL |error|:(missing value)
  end if
end convertMovieAt:ToType:withExtension:deleteOriginal:

on retFileFormatUTI(aExt as string)
  set aUTIstr to getUTIfromNameExtension(aExt) of me
  
set keyList to {current application’s AVFileTypeQuickTimeMovie, current application’s AVFileTypeMPEG4, current application’s AVFileTypeAppleM4V, current application’s AVFileTypeAppleM4A, current application’s AVFileType3GPP, current application’s AVFileType3GPP2, current application’s AVFileTypeCoreAudioFormat, current application’s AVFileTypeWAVE, current application’s AVFileTypeAIFF, current application’s AVFileTypeAIFC, current application’s AVFileTypeAMR, current application’s AVFileTypeMPEGLayer3, current application’s AVFileTypeSunAU, current application’s AVFileTypeAC3}
  
set valList to {“com.apple.quicktime-movie”, “public.mpeg-4″, “com.apple.m4v-video”, “com.apple.m4a-audio”, “public.3gpp”, “public.3gpp2″, “com.apple.coreaudio-format”, “com.microsoft.waveform-audio”, “public.aiff-audio”, “public.aifc-audio”, “org.3gpp.adaptive-multi-rate-audio”, “public.mp3″, “public.au-audio”, “public.ac3-audio”}
  
if aUTIstr is not in valList then
    error “Invalid Constant String”
  end if
  
  
set aPos to offsetOf(valList, aUTIstr) of me
  
set cRes to contents of item aPos of keyList
  
return cRes
end retFileFormatUTI

on offsetOf(aList as list, aTarg)
  set aArray to current application’s NSArray’s arrayWithArray:aList
  
set aIndex to (aArray’s indexOfObject:aTarg) as number
  
return (aIndex + 1)
end offsetOf

–ファイル拡張子からUTI文字列を取得する
on getUTIfromNameExtension(anExtStr as string)
  set aTempPath to (POSIX path of (path to temporary items from user domain))
  
set aTempName to (current application’s NSUUID’s UUID()’s UUIDString()) as string
  
set bTempPath to (aTempPath & aTempName & “.” & anExtStr)
  
do shell script “touch “ & quoted form of bTempPath
  
set utiStr to current application’s NSWorkspace’s sharedWorkspace()’s typeOfFile:bTempPath |error|:(missing value)
  
do shell script “rm -f “ & quoted form of bTempPath
  
return utiStr as string
end getUTIfromNameExtension

★Click Here to Open This Script 

2016/10/24 指定のムービーファイルのエクスポート可能な形式一覧を取得する

指定のムービーファイルのエクスポート(書き出し)可能なファイル形式一覧を取得するAppleScriptです。

AppleScript名:指定のムービーファイルのエクスポート可能な形式一覧を取得する
– Created 2016-10-24 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AVFoundation"
–http://piyocast.com/as/archives/4285

set posixPath to POSIX path of (choose file with prompt "Choose an Movie file:")
set theURL to current application’s |NSURL|’s fileURLWithPath:posixPath
set theAsset to current application’s AVAsset’s assetWithURL:theURL
set allowedPresets to current application’s AVAssetExportSession’s exportPresetsCompatibleWithAsset:theAsset
–>  (NSArray) {"AVAssetExportPreset1920×1080", "AVAssetExportPresetLowQuality", "AVAssetExportPresetAppleM4V720pHD", "AVAssetExportPresetAppleM4VAppleTV", "AVAssetExportPresetAppleM4A", "AVAssetExportPreset640×480", "AVAssetExportPresetAppleProRes422LPCM", "AVAssetExportPreset3840×2160", "AVAssetExportPresetAppleM4VWiFi", "AVAssetExportPresetHighestQuality", "AVAssetExportPresetAppleM4VCellular", "AVAssetExportPreset1280×720", "AVAssetExportPresetMediumQuality", "AVAssetExportPresetAppleM4V1080pHD", "AVAssetExportPresetAppleM4V480pSD", "AVAssetExportPreset960×540", "AVAssetExportPresetAppleM4ViPod"}

★Click Here to Open This Script 

2015/11/09 ムービーからオーディオのみ抽出してm4aで保存 v2

指定のQuickTimeムービーからオーディオのみ抽出してm4a(MPEG-4 Audio)で保存するAppleScriptです。

Shane Stanleyから「外部のFrameworkを使わなくても、AppleScriptだけで処理できる」バージョンを教えてもらいました。なるほど、たしかに外部のFrameworkを利用しなくてもexportできることを確認しました。

本Scriptは2本ともForegroundで実行する必要があるため、Script EditorでCommand-Control-Rで実行します。ASObjC Explorer 4では、「Run in Foreground」にチェックを入れて実行します。

AppleScript名:ASOCでムービーからオーディオのみ抽出してM4Aで保存 v2a
– Created 2015-11-09 by Shane Stanley
use AppleScript version “2.3.1″
use scripting additions
use framework “Foundation”
use framework “AVFoundation”

set aFile to POSIX path of (choose file of type {“com.apple.quicktime-movie”} with prompt “Choose a QuickTime movie to export as MP4 audio”)
set anNSURL to current application’s class “NSURL”’s fileURLWithPath:aFile
set outNSURL to anNSURL’s URLByAppendingPathExtension:“m4a”

set theAsset to current application’s AVAsset’s assetWithURL:anNSURL
set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:(current application’s AVAssetExportPresetAppleM4A)
theSession’s setOutputURL:outNSURL
theSession’s setOutputFileType:(current application’s AVFileTypeAppleM4A)
theSession’s exportAsynchronouslyWithCompletionHandler:(missing value)
current application’s NSTimer’s scheduledTimerWithTimeInterval:2.0 target:me selector:“checkSession:” userInfo:theSession repeats:true

on checkSession:theTimer
  set theSession to theTimer’s userInfo()
  
set theProgress to theSession’s status() as integer
  
if theProgress > 2 then
    theTimer’s invalidate()
    
if theProgress = (current application’s AVAssetExportSessionStatusCompleted) as integer then
      display dialog “Done”
    else if theProgress = (current application’s AVAssetExportSessionStatusFailed) as integer then
      display dialog “Failed”
    else if theProgress = (current application’s AVAssetExportSessionStatusCancelled) as integer then
      display dialog “Cancelled”
    end if
  end if
end checkSession:

★Click Here to Open This Script 

AppleScript名:ASOCでムービーからオーディオのみ抽出してM4Aで保存 v2b
– Created 2015-11-09 by Shane Stanley
– Modified 2015-11-09 by Takaaki Naganoya
use AppleScript version “2.3.1″
use scripting additions
use framework “Foundation”
use framework “AVFoundation”

property expStat : false

set aFile to POSIX path of (choose file of type {“com.apple.quicktime-movie”} with prompt “Choose a QuickTime movie to export as MP4 audio”)
set anNSURL to current application’s class “NSURL”’s fileURLWithPath:aFile
set outNSURL to anNSURL’s URLByAppendingPathExtension:“m4a”

set theAsset to current application’s AVAsset’s assetWithURL:anNSURL
set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:(current application’s AVAssetExportPresetAppleM4A)
theSession’s setOutputURL:outNSURL
theSession’s setOutputFileType:(current application’s AVFileTypeAppleM4A)
theSession’s exportAsynchronouslyWithCompletionHandler:(missing value)
current application’s NSTimer’s scheduledTimerWithTimeInterval:1.0 target:me selector:“checkSession:” userInfo:theSession repeats:true

repeat
  delay 0.01
  
if my expStat is not equal to false then exit repeat
end repeat

return my expStat
–>  ”Done”

on checkSession:theTimer
  set theSession to theTimer’s userInfo()
  
set theProgress to theSession’s status() as integer
  
if theProgress > 2 then
    theTimer’s invalidate()
    
if theProgress = (current application’s AVAssetExportSessionStatusCompleted) as integer then
      set my expStat to “Done”
    else if theProgress = (current application’s AVAssetExportSessionStatusFailed) as integer then
      set my expStat to “Failed”
    else if theProgress = (current application’s AVAssetExportSessionStatusCancelled) as integer then
      set my expStat to “Cancelled”
    end if
  end if
end checkSession:

★Click Here to Open This Script