20260518

[ffmpeg]メタデータの設定(ファイル名をタイトルに設定する)

[ffmpeg]メタデータの設定(ファイル名をタイトルに設定する)

NOTE記事一覧ですnote.com

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

ファイル名をタイトルに.applescript.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005
006FFMEPGでメタデータをセットする
007https://note.com/quicktimer/n/n50092fdb55fc
008
009FFMPEGのインストールはこちらをみてください
010https://note.com/quicktimer/n/n1712711c41cd
011
012
013
014
015com.cocolog-nifty.quicktimer.icefloe *)
016----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
017use AppleScript version "2.8"
018use framework "Foundation"
019use framework "AppKit"
020use scripting additions
021property refMe : a reference to current application
022set appFileManager to refMe's NSFileManager's defaultManager()
023
024################################
025#設定項目
026#一般的にはこちら
027#set strFfmpegFilePath to ("/usr/local/bin/ffmpeg") as text
028#################################
029#ffmpeg へのパス
030set strFfmpegFilePath to ("~/Library/Application Support/bin/ffmpeg/ffmpeg") as text
031set ocidFfmpegFilePathStr to refMe's NSString's stringWithString:(strFfmpegFilePath)
032set ocidFfmpegFilePathStr to ocidFfmpegFilePathStr's precomposedStringWithCanonicalMapping()
033set ocidFfmpegFilePath to ocidFfmpegFilePathStr's stringByStandardizingPath()
034set ocidFfmpegFilePath to ocidFfmpegFilePath's stringByExpandingTildeInPath()
035set ocidFfmpegFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFfmpegFilePath) isDirectory:(false)
036set strFfmpegFilePath to ocidFfmpegFilePathURL's |path|()
037
038#################################
039#ffprobe へのパス
040#一般的にはこちら
041#set strFfprobeFilePath to ("/usr/local/bin/ffprobe") as text
042set strFfprobeFilePath to ("~/Library/Application Support/bin/ffmpeg/ffprobe") as text
043set ocidFfprobeFilePathStr to refMe's NSString's stringWithString:(strFfprobeFilePath)
044set ocidFilePathStr to ocidFfprobeFilePathStr's precomposedStringWithCanonicalMapping()
045set ocidFfprobeFilePath to ocidFfprobeFilePathStr's stringByStandardizingPath()
046set ocidFfprobeFilePath to ocidFfprobeFilePath's stringByExpandingTildeInPath()
047set ocidFfprobeFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFfprobeFilePath) isDirectory:(false)
048set ocidFfprobeFilePath to ocidFfprobeFilePathURL's |path|()
049
050
051################################
052####BINPATH  ffmpegのパス
053################################
054#有無チェック
055set boolExists to appFileManager's fileExistsAtPath:(ocidFfmpegFilePath) isDirectory:(false)
056if boolExists is true then
057   log "【OK】指定のパスにFFMPEGがあります"
058else if boolExists is false then
059   return "【NG】指定のパスにFFMPEGがありません"
060end if
061
062################################
063#####書き出し先パス Moviesフォルダ内へ
064################################
065set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSMoviesDirectory) inDomains:(refMe's NSUserDomainMask))
066set ocidMoviesDirPathURL to ocidURLsArray's firstObject()
067set ocidSaveDirPathURL to (ocidMoviesDirPathURL's URLByAppendingPathComponent:("_FFMPEG書出"))
068set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
069ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
070set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
071################################
072#####ダイアログを前面に
073################################
074tell current application
075   set strName to name as text
076end tell
077if strName is "osascript" then
078   tell application "Finder" to activate
079else
080   tell current application to activate
081end if
082
083tell application "Finder"
084   set aliasDefaultLocation to container of (path to me) as alias
085end tell
086
087tell application "SystemUIServer"
088   activate
089   set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type {"public.movie"} with invisibles and showing package contents without multiple selections allowed) as alias
090end tell
091##
092set strFilePath to (POSIX path of aliasFilePath) as text
093set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
094set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
095set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
096#保存先
097set ocidFileName to ocidFilePathURL's lastPathComponent()
098set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName)
099set ocidSaveFilePath to ocidSaveFilePathURL's |path|()
100##ファイル名を取得(拡張子を除く)
101set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
102set strFileName to ocidBaseFilePathURL's lastPathComponent() as text
103#
104set strSetBinFilePath to doPathEscape(strFfmpegFilePath)
105set strSetFilePath to doPathEscape(strFilePath)
106set strSetSaveFilePathURL to doPathEscape(ocidSaveFilePath)
107set strSetFileName to doPathEscape(strFileName)
108########################################
109#ファイル名にする場合
110set strMetaTitle to ("-metadata title=\"" & strSetFileName & "\"") as text
111##タイトルだけの場合
112set strCmd to ("\"" & strSetBinFilePath & "\" -i \"" & strSetFilePath & "\" " & strMetaTitle & " -codec:v copy -codec:a copy   \"" & strSetSaveFilePathURL & "\"") as text
113
114(*
115#タイトルを入力して行う場合
116set strMetaTitle to ("-metadata title=\"-\"") as text
117set strSetMeta to ("" & strMetaTitle & " " & "-metadata artist=\"-\"") as text
118set strSetMeta to ("" & strSetMeta & " " & "-metadata album=\"-\"") as text
119set strSetMeta to ("" & strSetMeta & " " & "-metadata copyright=\"-\"") as text
120set strSetMeta to ("" & strSetMeta & " " & "-metadata comment=\"-\"") as text
121set strSetMeta to ("" & strSetMeta & " " & "-metadata author=\"-\"") as text
122set strSetMeta to ("" & strSetMeta & " " & "-metadata genre=\"-\"") as text
123set strSetMeta to ("" & strSetMeta & " " & "-metadata description=\"-\"") as text
124##入力して設定する場合
125set strCmd to ("\"" & strSetBinFilePath & "\" -i \"" & strSetFilePath & "\" " & strSetMeta & " -codec:v copy -codec:a copy   \"" & strSetSaveFilePathURL & "\"") as text
126*)
127
128do shell script strCmd
129
130#保存先を開く
131set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
132set listDone to appShardWorkspace's openURL:(ocidSaveDirPathURL)
133set appFinderArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:("com.apple.finder")
134set appFinder to appFinderArray's firstObject()
135set boolDone to appFinder's activateWithOptions:(refMe's NSApplicationActivateIgnoringOtherApps)
136return boolDone
137
138
139return listDone
140
141
142########################
143#パスのエスケープ
144to doPathEscape(strFilePath)
145   set strFilePath to strFilePath as text
146   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
147   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
148   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
149   set listEscChar to {"\\", "\"", "$", "`"} as list
150   repeat with itemEscChar in listEscChar
151      set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\" & itemEscChar & ""))
152   end repeat
153   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\""))
154   
155   return ocidFilePath
156end doPathEscape
AppleScriptで生成しました