[DaVinci Resolve]DaVinci Resolveから書き出したVTTにLineポジションを付与する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | (* |
005 | VTTフォーマットの字幕に |
006 | ポジション情報を付与します |
007 | Youtube用 |
008 | *) |
009 | # com.cocolog-nifty.quicktimer.icefloe |
010 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
011 | use AppleScript version "2.8" |
012 | use framework "Foundation" |
013 | use framework "AppKit" |
014 | use scripting additions |
015 | |
016 | property refMe : a reference to current application |
017 | |
018 | |
019 | ######################## |
020 | ##設定項目 |
021 | set strSetLinePos to (" line:20%") as text |
022 | |
023 | ######################## |
024 | ##ダイアログ |
025 | ######################## |
026 | set strName to (name of current application) as text |
027 | if strName is "osascript" then |
028 | tell application "Finder" to activate |
029 | else |
030 | tell current application to activate |
031 | end if |
032 | # |
033 | set appFileManager to refMe's NSFileManager's defaultManager() |
034 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
035 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
036 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
037 | # |
038 | set listUTI to {"org.w3.webvtt"} as list |
039 | set strMes to ("ファイルを選んでください") as text |
040 | set strPrompt to ("ファイルを選んでください") as text |
041 | try |
042 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
043 | on error |
044 | log "エラーしました" |
045 | return "エラーしました" |
046 | end try |
047 | #入力パス |
048 | set strFilePath to (POSIX path of aliasFilePath) as text |
049 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
050 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
051 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
052 | #出力パス |
053 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
054 | set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("append.vtt") |
055 | ######################## |
056 | ##テキスト処理 |
057 | ######################## |
058 | #テキスト読み込み |
059 | set listResponse to refMe's NSMutableString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
060 | if (item 2 of listResponse) = (missing value) then |
061 | log "initWithContentsOfURL 正常処理" |
062 | set ocidReadString to (item 1 of listResponse) |
063 | else if (item 2 of listResponse) ≠ (missing value) then |
064 | log (item 2 of listResponse)'s code() as text |
065 | log (item 2 of listResponse)'s localizedDescription() as text |
066 | return "initWithContentsOfURL エラーしました" |
067 | end if |
068 | #改行コードをUNIXに強制 |
069 | set ocidReplacedStrings to ocidReadString's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n") |
070 | set ocidReplacedStrings to ocidReplacedStrings's stringByReplacingOccurrencesOfString:("\r") withString:("\n") |
071 | |
072 | ######################## |
073 | ##事前処理 DaVinci Resolve用 |
074 | ######################## |
075 | set ocidReplacedStrings to ocidReplacedStrings's stringByReplacingOccurrencesOfString:("<b>") withString:("") |
076 | set ocidReplacedStrings to ocidReplacedStrings's stringByReplacingOccurrencesOfString:("</b>") withString:("") |
077 | #歌詞の場合用 |
078 | set ocidReplacedStrings to ocidReplacedStrings's stringByReplacingOccurrencesOfString:("WEBVTT\n\n") withString:("WEBVTT\nKind: lyric\nLanguage: ja\n\n") |
079 | #通常の字幕用 |
080 | #set ocidReplacedStrings to ocidReplacedStrings's stringByReplacingOccurrencesOfString:("WEBVTT\n\n") withString:("WEBVTT\nKind: subtitles\nLanguage: ja\n\n") |
081 | |
082 | ######################## |
083 | ##本処理 |
084 | ######################## |
085 | #改行区切りでリストに |
086 | set ocidLineTextArray to ocidReplacedStrings's componentsSeparatedByString:("\n") |
087 | #リストの数 |
088 | set numCntArray to (ocidLineTextArray's |count|()) as integer |
089 | #出力用のテキスト |
090 | set ocidOutPutString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
091 | #行の内容判定用Charset |
092 | set ocidNewLine to (refMe's NSCharacterSet's decimalDigitCharacterSet's invertedSet()) |
093 | |
094 | |
095 | #読み込んだテキストの行数だけ繰り返し |
096 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
097 | ##リストから対象の行のテキスト読み込み |
098 | set ocidLineText to (ocidLineTextArray's objectAtIndex:(itemNo)) |
099 | # |
100 | if (ocidLineText as text) is "" then |
101 | (ocidOutPutString's appendString:("\n\n")) |
102 | else if (ocidLineText as text) contains " --> " then |
103 | (ocidOutPutString's appendString:(ocidLineText)) |
104 | (ocidOutPutString's appendString:(strSetLinePos)) |
105 | (ocidOutPutString's appendString:("\n")) |
106 | else |
107 | (ocidOutPutString's appendString:(ocidLineText)) |
108 | (ocidOutPutString's appendString:("\n")) |
109 | end if |
110 | |
111 | end repeat |
112 | ######################## |
113 | ##保存 |
114 | ######################## |
115 | set listDone to (ocidOutPutString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
116 | if (item 1 of listDone) is true then |
117 | return "writeToURL 正常終了" |
118 | else if (item 2 of listDone) ≠ (missing value) then |
119 | log (item 2 of listDone)'s code() as text |
120 | log (item 2 of listDone)'s localizedDescription() as text |
121 | return "writeToURL エラーしました" |
122 | end if |
123 | |
124 | |
125 | |
AppleScriptで生成しました |
| 固定リンク
「YouTube」カテゴリの記事
- [DaVinci Resolve]DaVinci Resolveから書き出したVTTにLineポジションを付与する(2024.07.14)
- [Youtube API]チャンネルIDからチャンネルのアップロードビデオを取得する(2024.03.15)
- [YoutubeAPI] チャンネルIDからチャンネルのアップロードビデオのリストIDを取得する(2024.03.14)
- [YoutubeAPI] チャンネルIDからカスタムURL=ユーザー名(アカウント名)を取得(2024.03.14)
- [Youtube]ビデオIDから主要な情報を取得(2024.01.13)