SSブログ

iTunes に登録した動画ファイルをメモリーカードへアートワークと一緒にコピー [Mac]

アップルがなかなかビデオiPodの新型を出してくれないので、携帯の動画再生機能を
使っているのだが、iTunes に登録した動画ファイルを手で携帯にコピーするのが
面倒になってきたので、スクリプトをあちこちから借りてきてくっつけて
でっち上げてみた。今の携帯はメモリースティックDuoスロット付きなので、
それが対象。

以下は、Automator の「アップルスクリプトを実行」アクションの中身で、
「選択されたiTunes項目の取得」アクションに接続して保存し、
iTunes でコピーする動画を選択後、スクリプトメニューから実行する。

アップルスクリプト単体では動かず、もう一つ Perl スクリプトが必要。

最初に宛先のメモリースティックの動画を全部消すので注意。

メモリースティックには、"MEMORYSTICK"というボリューム名がついていること。
別にSDカードでもUSBマスストレージでも、宛先フォルダやファイル名を
変更してやれば転用可能か。

on run {input, parameters}
	
	-- 危険:
	-- スクリプト実行ごとに全入れ替えが前提なので、
	-- メモリースティックの動画フォルダの中身を全て消去します。
	try
		do shell script "rm /Volumes/MEMORYSTICK/MP_ROOT/100MNV01/*.MP4"
		do shell script "rm /Volumes/MEMORYSTICK/MP_ROOT/100MNV01/*.THM"
	end try
	
	-- growl のサンプルからのコピー
	-- 本当は、growl がインストールされているかどうか確認すべきですが、
	-- してません。
	--tell application "System Events"
	--	set isRunning to ¬
	--		count of (every process whose name is "GrowlHelperApp") > 0
	--end tell
	
	-- growl のサンプルからのコピー
	-- このスクリプトを growl に登録します。 
	tell application "GrowlHelperApp"
		-- Make a list of all the notification types 
		-- that this script will ever send:
		set the allNotificationsList to ¬
			{"Copying Notification", "All done Notification"}
		
		-- Make a list of the notifications 
		-- that will be enabled by default.      
		-- Those not enabled by default can be enabled later 
		-- in the 'Applications' tab of the growl prefpane.
		set the enabledNotificationsList to ¬
			{"Copying Notification", "All done Notification"}
		
		-- Register our script with growl.
		-- You can optionally (as here) set a default icon 
		-- for this script's notifications.
		register as application ¬
			"iTunes Movie to MS COPY" all notifications allNotificationsList ¬
			default notifications enabledNotificationsList ¬
			icon of application "Script Editor"
		
	end tell
	
	-- 以下の処理を、iTunesで選択されている項目に対して実行します。
	-- 1. cover art を取得して、リサイズしてサムネイルとして保存
	-- 2. 動画ファイル本体と、サムネイルファイルをメモリースティックの動画フォルダへコピー
	-- 3. 一項目処理開始ごとに growl で通知、終了時に通知

	set copytarget to {}
	
	-- 先に iTunes から項目を取得して保存して、後でコピーを実施
	tell application "iTunes"
		repeat with i in input
			if (class of i is file track) then
				set ThisArtWork to (data of artwork 1 of i) as picture
				--set data of artwork 1 of i to ThisArtWork
				set trackname to name of i
				
				--再生回数インクリメント
				set trackcount to played count of i
				set trackcount to trackcount + 1
				set played count of i to trackcount
				
				set x to location of i
				set j to POSIX path of x
				
				-- trackname pictimage pathname のタプルをリストに保存したい
				set end of copytarget to {trackname, j, ThisArtWork}
				
				-- cover art を一時ファイルに保存
				set u to j & ".pict" as string
				set pictname to u as POSIX file
				set fd to open for access pictname with write permission
				set eof of the fd to 0
				write ThisArtWork to fd starting at eof
				close access fd
				
				-- 一時ファイルのデータをスケーリングして拡張子を変更し保存
				set v to j & ".THM" as string
				set jpegname to v as POSIX file
				do shell script "tail -c +223 " & quoted form of u & " > " & v
				do shell script "sips --resampleWidth 160 " & quoted form of v
				do shell script "rm " & quoted form of u
			end if
		end repeat
	end tell
	
	
	repeat with copyitem in copytarget
		-- ここから先を itunes tell からはずす
		set trackname to item 1 of copyitem
		set ThisArtWork to item 3 of copyitem as picture
		set j to item 2 of copyitem
		
		tell application "GrowlHelperApp"
			--	Send a Notification...
			notify with name ¬
				"Copying Notification" title ¬
				"Now copying..." description ¬
				trackname application name "iTunes Movie to MS COPY" pictImage ThisArtWork
		end tell
		
		
		-- メモリースティックの動画フォルダへ、連番をずらしながらファイルを
		-- Perl スクリプトでコピー
		with timeout of 10800 seconds
			do shell script "~/Applications/cms " & quoted form of j & " /Volumes/MEMORYSTICK/MP_ROOT/100MNV01"
		end timeout
	end repeat
	
	
	-- growl でコピー終了の通知
	tell application "GrowlHelperApp"
		notify with name ¬
			"All done Notification" title ¬
			"All done :) " description ¬
			"Alas — you won't see me until you enable me..." application name "iTunes Movie to MS COPY"
		
	end tell
	
	-- 意味なし
	return alias jpegname
end run

こちらが Perl スクリプト。"cms" と名前をつけて、~/Applications へでも保存。

#!/usr/bin/perl

$i = 1;
$src = $ARGV[0];
$dest = $ARGV[1];
$mp4_ext = ".MP4";
$thumb_ext = ".THM";

while (1) {
        $destname = $dest;
        $destname = $dest . sprintf("/M4V%05d", $i++);
        $destname_mp4 = $destname . $mp4_ext;
        $destname_thumb = $destname . $thumb_ext;
        $srcname = $src;
        $srcname_thumb = $srcname . $thumb_ext;

        if (!(-e $dest) || !(-d $dest)) {
                print "$dest not exist.\n";
                exit 0;
        }

        if (!(-e $srcname)) {
                print "$srcname not exist.\n";
                exit 0;
        }

        if (!(-e $destname_mp4)) {
                print $destname_mp4 . "\n";
                print $destname_thumb . "\n";
                system "cp $srcname $destname_mp4";
                system "cp $srcname_thumb $destname_thumb";
                last;
        }
}

exit 0;

ちゃんとサイズ計算して入るかどうか見るべきなんだけどアップルスクリプトも
Perl なみによくわからないのでこれで良しとした。

しかし、iTunes が動画に付けるアートワーク、フレームを後からiTunesで選びなおせないのだろうか。

3/21追記:
1. 転送時にiTunes側の再生回数を1増加させるようにスクリプトを修正。
スマートプレイリストで未視聴コンテンツ(played count == 0)をメモリーカードの
サイズ未満でランダムに選ばさせるために設定。

2. スクリプト内のiTunes 操作と、実際にファイルをメモリーカードへコピーする部分を分離。
iTunes に tell してる間に時間がかかりすぎると、いろいろおかしなことになるため。


nice!(0)  コメント(0)  トラックバック(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。