投稿日:2025-10-14

AppleScriptでffmpeg動画エンコード

昔のQuickTime7時代はPro版にすればちょっとした動画編集も気軽にできたのですが、今はトリミングするくらいしか編集機能がなく、いちいちiMovieに取り込まなくてはならず面倒になってしまいました。

変換のためのソフトもいくつか出て入るのですが、普段このブログに載せたりと大した事もしていないので、ffmpegを使ってエンコードをするAppleScriptをChatGPTに作って貰いました。

昔はChatGPTもAppleScriptの学習が少なくイマイチだったのですが、最近はだいぶ良くなっています。このコードも一発生成。見直していないのでバグや潜在的な問題もあるかもしれませんので、ご利用の際はご注意。

ffmpegはターミナルから

brew install ffmpeg

Homebrewでインストールできます。

このAppleScriptの機能一覧

コードはこちら

もっと見る…
-- mov → mp4 変換ランチャー(ffmpeg 固定パス版)
-- ffmpeg: /opt/homebrew/bin/ffmpeg を使用
-- 使い方:実行 → 入力ファイル選択(複数OK)→ 出力フォルダ → モード/解像度/品質指定 → 実行
on run
	try
		-- ffmpeg の固定パス
		set ffmpegPath to "/opt/homebrew/bin/ffmpeg"
		set ffmpegExists to do shell script "test -x " & quoted form of ffmpegPath & " && echo 1 || echo 0"
		if ffmpegExists is "0" then
			display dialog "ffmpegが見つかりませんでした。
想定パス: " & ffmpegPath buttons {"OK"} default button 1 with icon caution
			return
		end if
		
		-- 変換する動画を選択(複数可)
		set theFiles to choose file with prompt "変換したい .mov / .mp4 などの動画を選んでね(複数可)" of type {"public.movie"} with multiple selections allowed
		-- 出力先
		set destFolder to choose folder with prompt "書き出し先フォルダを選んでね"
		set destPOSIX to POSIX path of destFolder
		
		-- 変換モード
		set modeChoices to {"コピー(再エンコードなし・超高速)", "H.264再エンコード:CRF(画質重視)", "H.264再エンコード:ビットレート指定"}
		set pickedMode to choose from list modeChoices with prompt "変換モードを選んでね" default items {"コピー(再エンコードなし・超高速)"} without multiple selections allowed
		if pickedMode is false then return
		set pickedMode to item 1 of pickedMode
		
		-- 解像度
		set resChoices to {"オリジナル", "720p(高さ720)", "1080p(高さ1080)"}
		set pickedRes to choose from list resChoices with prompt "出力解像度を選んでね" default items {"オリジナル"} without multiple selections allowed
		if pickedRes is false then return
		set pickedRes to item 1 of pickedRes
		
		-- 追加パラメータ
		set crfValue to ""
		set vBitrate to ""
		set vBitrateInt to 0
		set bufsizeStr to ""
		if pickedMode is "H.264再エンコード:CRF(画質重視)" then
			set crfValue to text returned of (display dialog "CRF値(推奨 18〜24。小さいほど高画質)" default answer "20")
		else if pickedMode is "H.264再エンコード:ビットレート指定" then
			set vBitrate to text returned of (display dialog "映像ビットレート(kbps)を入力(例:5000)" default answer "5000")
			try
				set vBitrateInt to vBitrate as integer
			on error
				display dialog "ビットレートは数値で入力してね(例:5000)" buttons {"OK"} default button 1 with icon caution
				return
			end try
			set bufsizeStr to ((vBitrateInt * 2) as string) & "k"
		end if
		
		-- 解像度フィルタ(-2指定で偶数幅維持)
		set scalePart to ""
		if pickedRes starts with "720p" then
			set scalePart to " -vf scale=-2:720"
		else if pickedRes starts with "1080p" then
			set scalePart to " -vf scale=-2:1080"
		end if
		
		-- 実行ループ
		set totalN to (count of theFiles)
		set doneN to 0
		repeat with f in theFiles
			set inPOSIX to POSIX path of f
			set baseName to do shell script "basename " & quoted form of inPOSIX & " | sed 's/\\.[^.]*$//'"
			set outPath to destPOSIX & baseName & ".mp4"
			set outPath to uniquePath(outPath)
			
			set cmd to ""
			if pickedMode is "コピー(再エンコードなし・超高速)" then
				-- 中身がH.264/AACなどmp4互換なら爆速。非互換ならエラーになることあり
				set cmd to quoted form of ffmpegPath & " -y -i " & quoted form of inPOSIX & " -c copy -movflags +faststart " & quoted form of outPath
			else if pickedMode is "H.264再エンコード:CRF(画質重視)" then
				set cmd to quoted form of ffmpegPath & " -y -i " & quoted form of inPOSIX & scalePart & " -c:v libx264 -preset veryfast -crf " & crfValue & " -c:a aac -b:a 160k -movflags +faststart " & quoted form of outPath
			else if pickedMode is "H.264再エンコード:ビットレート指定" then
				set cmd to quoted form of ffmpegPath & " -y -i " & quoted form of inPOSIX & scalePart & " -c:v libx264 -preset veryfast -b:v " & vBitrate & "k -maxrate " & vBitrate & "k -bufsize " & bufsizeStr & " -c:a aac -b:a 160k -movflags +faststart " & quoted form of outPath
			end if
			
			do shell script cmd
			
			set doneN to doneN + 1
			display notification (baseName & ".mp4 を出力しました(" & doneN & " / " & totalN & ")") with title "ffmpeg 変換"
		end repeat
		
		display dialog "変換が完了しました!
出力先: " & destPOSIX buttons {"OK"} default button 1 with icon note
		
	on error errMsg number errNum
		display dialog "エラーが発生しました:
" & errMsg & " (" & errNum & ")" buttons {"OK"} default button 1 with icon stop
	end try
end run
-- 既存ファイルと重なる場合に -1, -2… を付けて回避
on uniquePath(targetPath)
	set qPath to quoted form of targetPath
	set existsFlag to do shell script "test -e " & qPath & " && echo 1 || echo 0"
	if existsFlag is "0" then return targetPath
	
	set baseNoExt to do shell script "p=" & qPath & "; b=$(basename \"$p\"); d=$(dirname \"$p\"); n=${b%.*}; ext=${b##*.}; i=1; while [ -e \"$d/${n}-\"$i\".$ext\" ]; do i=$((i+1)); done; printf \"%s/%s-%d.%s\" \"$d\" \"$n\" \"$i\" \"$ext\""
	return baseNoExt
end uniquePath

ジャンル: IT カテゴリー: プログラム

← 記事一覧に戻る