| 001 | #!/usr/bin/env osascript |
| 002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 003 | (* |
| 004 | NASAのビデオのダウンローダー |
| 005 | ファイルがTSの分割形式で配信されているビデオ用 |
| 006 | m3u8ファイルからTSファイル名を取得してダウンロードします |
| 007 | mp4ビデオにするには |
| 008 | ffmpegが必要です |
| 009 |
|
| 010 | com.cocolog-nifty.quicktimer.icefloe *) |
| 011 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 012 | use AppleScript version "2.8" |
| 013 | use framework "Foundation" |
| 014 | use framework "AppKit" |
| 015 | use scripting additions |
| 016 | property refMe : a reference to current application |
| 017 | ######################## |
| 018 | #設定項目 URL |
| 019 | set strM3Uurl to ("https://nasaplus.akamaized.net/output/10029-7.m3u8") |
| 020 | (* |
| 021 | 99999-7_00000.ts: 1920 × 1080 |
| 022 | 99999-6_00000.ts: 1280 × 720 |
| 023 | 99999-5_00000.ts: 1280 × 720 |
| 024 | 99999-4_00000.ts: 960 × 540 |
| 025 | 99999-3_00000.ts: 640 × 360 |
| 026 | 99999-2_00000.ts: 640 × 360 |
| 027 | 99999-1_00000.ts: 480 × 270 |
| 028 | *) |
| 029 | ######################## |
| 030 | #設定項目 ffmpeg |
| 031 | set strBinPath to ("~/Library/Application Support/bin/ffmpeg/ffmpeg") as text |
| 032 | ######################## |
| 033 | #URL組み立て |
| 034 | set ocidURLString to refMe's NSString's stringWithString:(strM3Uurl) |
| 035 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString) |
| 036 | set strURL to ocidURL's absoluteString() as text |
| 037 | set ocidHostName to ocidURL's |host|() as text |
| 038 | set ocidScheme to ocidURL's |scheme|() as text |
| 039 | set ocidBaseURL to ocidURL's URLByDeletingLastPathComponent() |
| 040 | set ocidLastPath to ocidBaseURL's lastPathComponent() |
| 041 | ######################## |
| 042 | #テンポラリーダウンロードフォルダ |
| 043 | #起動時に削除される項目内に作成 |
| 044 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 045 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
| 046 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
| 047 | set ocidUUIDString to ocidUUID's UUIDString |
| 048 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true) |
| 049 | # |
| 050 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 051 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
| 052 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 053 |
|
| 054 | ######################## |
| 055 | #M3Uファイル整形 |
| 056 | # set ocidM3UFilePathStr to refMe's NSString's stringWithString:(strM3uFilePath) |
| 057 | # set ocidM3UFilePath to ocidM3UFilePathStr's stringByExpandingTildeInPath() |
| 058 | # set ocidM3UFilePathURL to refMe's NSURL's fileURLWithPath:(ocidM3UFilePath) |
| 059 | # |
| 060 | set listResponse to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
| 061 | set ocidReadString to (first item of listResponse) |
| 062 | # |
| 063 | set strLF to ("" & linefeed & "") as text |
| 064 | set strCR to ("" & return & "") as text |
| 065 | set strCRLF to ("" & return & linefeed & "") as text |
| 066 | set strSpace to ("" & space & "") as text |
| 067 | set strTab to ("" & tab & "") as text |
| 068 | #改行をUNIXに強制 |
| 069 | set ocidReadString to (ocidReadString's stringByReplacingOccurrencesOfString:(strCRLF) withString:(strLF)) |
| 070 | set ocidReadString to (ocidReadString's stringByReplacingOccurrencesOfString:(strCR) withString:(strLF)) |
| 071 | set ocidReadString to (ocidReadString's stringByReplacingOccurrencesOfString:(strLF & strLF) withString:(strLF)) |
| 072 | #改行でArrayにしておく |
| 073 | set ocidLineArray to ocidReadString's componentsSeparatedByString:(strLF) |
| 074 | #空行 コメント行削除 |
| 075 | set appPredicate to refMe's NSPredicate's predicateWithFormat:("NOT (self BEGINSWITH '#') AND self.length > 0") |
| 076 | set ocidLineArrayPre to ocidLineArray's filteredArrayUsingPredicate:(appPredicate) |
| 077 | #ソート |
| 078 | set ocidLineArray to ocidLineArrayPre's sortedArrayUsingSelector:("localizedStandardCompare:") |
| 079 | ######################## |
| 080 | #URLコンポーネント初期化 |
| 081 | set ocidURLComponents to refMe's NSURLComponents's alloc()'s init() |
| 082 | ocidURLComponents's setScheme:(ocidScheme) |
| 083 | ocidURLComponents's setHost:(ocidHostName) |
| 084 | set strBasePath to ("/" & ocidLastPath & "/") as text |
| 085 | ######################## |
| 086 | #ダウンロード処理 |
| 087 | set numCnter to 0 as integer |
| 088 | set numCntArray to ocidLineArray's |count|() |
| 089 | repeat with itemLine in ocidLineArray |
| 090 | if (itemLine as text) is "" then |
| 091 | exit repeat |
| 092 | end if |
| 093 | set boolHasPre to (itemLine's hasPrefix:("#")) as boolean |
| 094 | if boolHasPre is false then |
| 095 | #URL |
| 096 | set ocidFileName to itemLine |
| 097 | set strSetPath to ("" & strBasePath & ocidFileName & "") as text |
| 098 | (ocidURLComponents's setPath:(strSetPath)) |
| 099 | set ocidURL to ocidURLComponents's |URL|() |
| 100 | set strURL to ocidURL's absoluteString() as text |
| 101 | log strURL |
| 102 | #保存先 |
| 103 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)) |
| 104 | #NSDATA |
| 105 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
| 106 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)) |
| 107 | set ocidReadData to (first item of listResponse) |
| 108 | if ocidReadData = (missing value) then |
| 109 | |
| 110 | delay 5 |
| 111 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)) |
| 112 | set ocidReadData to (first item of listResponse) |
| 113 | end if |
| 114 | #NSDATA |
| 115 | set ocidOption to (refMe's NSDataWritingAtomic) |
| 116 | set listDone to (ocidReadData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)) |
| 117 | end if |
| 118 | log ("Done: " & numCnter & "/" & numCntArray & "") as text |
| 119 | set numCnter to (numCnter + 1) as integer |
| 120 | end repeat |
| 121 |
|
| 122 |
|
| 123 | ######################## |
| 124 | #コンテンツの収集 |
| 125 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
| 126 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s init() |
| 127 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
| 128 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidSaveDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error|:(reference)) |
| 129 | set ocidSubPathURLArray to (first item of listResponse) |
| 130 |
|
| 131 | ######################## |
| 132 | #マージファイル |
| 133 | set ocidMergeFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("merge.ts") isDirectory:(false) |
| 134 | set ocidMergeFilePath to ocidMergeFilePathURL's |path|() |
| 135 | set boolDone to appFileManager's createFileAtPath:(ocidMergeFilePath) |contents|:(missing value) attributes:(missing value) |
| 136 | ######################## |
| 137 | #ソート |
| 138 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:("localizedStandardCompare:") |
| 139 | set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor) |
| 140 | set ocidSortedArray to ocidSubPathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray) |
| 141 | ######################## |
| 142 | #マージ |
| 143 | set ocidMergeFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("merge.ts") isDirectory:(false) |
| 144 | set ocidMergeFilePath to ocidMergeFilePathURL's |path|() |
| 145 | set boolDone to appFileManager's createFileAtPath:(ocidMergeFilePath) |contents|:(missing value) attributes:(missing value) |
| 146 | # |
| 147 | set appHandleOut to refMe's NSFileHandle's fileHandleForWritingAtPath:(ocidMergeFilePath) |
| 148 | repeat with itemFileURL in ocidSortedArray |
| 149 | set ocidItemFile to itemFileURL's |path|() |
| 150 | set appHandleIn to (refMe's NSFileHandle's fileHandleForReadingAtPath:(ocidItemFile)) |
| 151 | set ocidItemData to appHandleIn's readDataToEndOfFile() |
| 152 | (appHandleOut's writeData:(ocidItemData)) |
| 153 | appHandleIn's closeFile() |
| 154 | end repeat |
| 155 | appHandleOut's closeFile() |
| 156 | ######################## |
| 157 | # |
| 158 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 159 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 160 | set ocidMP4FilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:("merge.mp4") isDirectory:(false) |
| 161 | set ocidMP4FilePath to ocidMP4FilePathURL's |path|() |
| 162 | # |
| 163 | set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath) |
| 164 | set ocidBinPath to ocidBinPathStr's stringByStandardizingPath() |
| 165 | #コマンド整形 |
| 166 | ##FIX用 タイムスケール1000だけ調整 |
| 167 | set strCmd to ("\"" & ocidBinPath & "\" -i \"" & ocidMergeFilePath & "\" -codec:v copy -codec:a copy -movflags +faststart -video_track_timescale 1000 \"" & ocidMP4FilePath & "\"") as text |
| 168 | do shell script strCmd |
| 169 | ######################## |
| 170 | #テンポラリーデータをゴミ箱へ |
| 171 | set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidSaveDirPathURL) |error|:(reference)) |
| 172 | return |