Нужно яблочный скрипт, чтобы получить общую продолжительность видео в выбранной папке - PullRequest
0 голосов
/ 03 мая 2018

У меня на самом деле яблочный скрипт, в котором приглашение запрашивает папку, чтобы получить общую продолжительность видео в этой папке, но вместо этого я хочу использовать этот яблочный скрипт как «Сервис», просто щелкнув правой кнопкой мыши по этой папке и выбрав мой сервис, который я получаю. продолжительность всех видео в этой папке. В таком случае я знаю, как сделать сервис из скрипта. Поэтому, пожалуйста, помогите мне, друзья.

-- time the following operation:
--set start_time to (time of (current date)) -- start timing

set this_folder to (choose folder with prompt "Pick the folder containing the video files to process:") as string

set video_ext_list to {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"}


tell application "Finder"
	set these_files to ((files of folder this_folder whose name extension is in video_ext_list) as alias list)
end tell

set filesCount to count of these_files

set total_duration_of_files_in_seconds to ""
set files_with_no_duration to 0

repeat with i from 1 to filesCount
	set this_file to (item i of my these_files)
	set this_info to info for this_file
	if visible of this_info is true and alias of this_info is false then
		-- insert actions here for: this_file
		set this_file_duration_in_seconds to do shell script "mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & quoted form of POSIX path of this_file
		if this_file_duration_in_seconds is not 0 then
			set total_duration_of_files_in_seconds to total_duration_of_files_in_seconds + this_file_duration_in_seconds
		else
			set files_with_no_duration to files_with_no_duration + 1
		end if
	end if
end repeat

set total_duration_in_hms to do shell script " echo " & total_duration_of_files_in_seconds & " | awk '{printf \"%03d:%02d:%02d\",$0/3600,$0%3600/60,$0%60}'"

(*
set end_time to (time of (current date)) -- stop timing.
set elapsed_time to end_time - start_time

log "2. elapsed_time is is " & elapsed_time & " seconds."
*)

display dialog "Files processed : " & (count of these_files) & return & "Files without duration : " & files_with_no_duration & return & "Total duration of " & (count of these_files) - files_with_no_duration & " files in h:m:s : " & total_duration_in_hms with title "Length of all videos in a folder" buttons {"OK"} default button 1

1 Ответ

0 голосов
/ 03 мая 2018
  • Запуск Automator
  • Нажмите кнопку New Document или нажмите ⌘N
  • Выберите Service и нажмите Choose
  • В области над холстом, в который можно перетащить действия, выполните следующие настройки: enter image description here
  • В поле поиска введите run ap
  • Перетащите действие Run AppleScript на холст
  • Заменить весь код на

    on run {input, parameters}
        set sourceFolder to item 1 of input
        set video_ext_list to {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"}
    
    
        tell application "Finder"
            set these_files to ((files of sourceFolder whose name extension is in video_ext_list) as alias list)
        end tell
    
        set filesCount to count of these_files
    
        set total_duration_of_files_in_seconds to ""
        set files_with_no_duration to 0
    
        repeat with i from 1 to filesCount
            set this_file to (item i of my these_files)
            set this_info to info for this_file
            if visible of this_info is true and alias of this_info is false then
                -- insert actions here for: this_file
                set this_file_duration_in_seconds to do shell script "mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & quoted form of POSIX path of this_file
                if this_file_duration_in_seconds is not "0" then
                    set total_duration_of_files_in_seconds to total_duration_of_files_in_seconds + this_file_duration_in_seconds
                else
                    set files_with_no_duration to files_with_no_duration + 1
                end if
            end if
        end repeat
    
        set total_duration_in_hms to do shell script " echo " & total_duration_of_files_in_seconds & " | awk '{printf \"%03d:%02d:%02d\",$0/3600,$0%3600/60,$0%60}'"
    
        (*
        set end_time to (time of (current date)) -- stop timing.
        set elapsed_time to end_time - start_time
    
        log "2. elapsed_time is is " & elapsed_time & " seconds."
        *)
    
        display dialog "Files processed : " & (count of these_files) & return & "Files without duration : " & files_with_no_duration & return & "Total duration of " & (count of these_files) - files_with_no_duration & " files in h:m:s : " & total_duration_in_hms with title "Length of all videos in a folder" buttons {"OK"} default button 1
    
    
        return input
    end run
    
  • Сохранить услугу

  • В Finder выберите папку и запустите службу из контекстного меню

Примечание: вы должны проверить строку "0", а не целое число 0 в

if this_file_duration_in_seconds is not "0" then

потому что скрипт оболочки всегда возвращает строки и is not 0 всегда будет иметь значение true

...