Renombrar episodios de iTunes TV para incluir la temporada y el número de episodio en el nombre del archivo

1

Estoy en el proceso de cambiar de iTunes al servidor Emby para transmitir videos a mi Apple TV. A Emby le va bien con las películas, pero para identificar episodios de series de televisión necesita la temporada y el número de episodio en el nombre del archivo.

Desafortunadamente, iTunes nombra archivos de video de episodios de TV usando solo el nombre del episodio. Tiene los números de los episodios en sus etiquetas y base de datos, pero no los coloca en el nombre del archivo. Así que una ubicación típica de archivo es:

TV Shows > Jeeves and Wooster > Season 1 > Jeeves Takes Charge.mp4

Pero lo que quiero es:

TV Shows > Jeeves and Wooster > Season 1 > Jeeves and Wooster S01E01.mp4

Incluso simplemente agregar el número del episodio al principio sería genial:

01 Jeeves Takes Charge.mp4

Mi biblioteca es lo suficientemente grande como para hacer que esto a mano se convierta en un sumidero bastante grande. ¿Se puede obligar a iTunes a hacerlo automáticamente? Si no es así, ¿existe una herramienta de terceros u otra solución lista para usar? Estoy seguro de que se puede hacer en Applescript con Exiftool, pero preferiría no reinventar la rueda si alguien más ya lo ha hecho.

    
pregunta Robert 10.07.2018 - 22:38

1 respuesta

1

Todavía no sé si hay una manera de obligar a iTunes a hacerlo automáticamente, pero lo logré con Applescript y ExifTool. En caso de que sea útil para alguien más, a continuación se muestra el script completo. Primero deberá instalar ExifTool . Puede ejecutar el script para elegir un archivo a la vez, o exportarlo como una aplicación para arrastrar y soltar.

Renombra episodios como:

Mostrar nombre S01E01 Episode Name.mp4

que es un formato reconocido por Emby, Plex y otros administradores de medios.

Procesa las carpetas de forma recursiva, por lo que puedes arrastrar toda la carpeta de programas de TV a una sola vez. Se omitirán los archivos que no sean de video o que no tengan los metadatos del programa de televisión esperados, y cuando se hace, aparece un cuadro de diálogo que le indica la cantidad de cada uno que encontró.

(*
Rename TV Episode to include Season and Number

Drop or select files or folders.
This script will check each file for metadata tags for TV show name, season number, episode number, and episode name and rename each file in the format:
Show Name S01E01 Episode Name.mp4

It will ignore files that don't match one of the extensions in the extensionList below. By default, mp4, m4v, mkv.
It will skip files that don't have all four of the desired meta tags.
If the show name or episode name contains illegal characters (colon, forward-slash, back-slash) it will replace them with "_" for the filename.

ExifTool must be installed. You can find it here:
https://www.sno.phy.queensu.ca/~phil/exiftool/

Make sure the pathToExifTool matches your system. The included one is the default.
*)

property pathToExifTool : "/usr/local/bin/exiftool"

property whitelistTypes : false
property whitelistExtensions : true
property whitelistTypeIDs : false

property typeList : {} -- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property extensionList : {"mp4", "m4v", "mkv", "avi", "wmv"} -- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}
property typeIDsList : {} -- eg: {"public.jpeg", "public.tiff", "public.png"}

global numberOfItemsRenamed
global numberOfItemsIgnored
global numberOfItemsWithoutMetaData

-- If opened without a drop, choose a file and simulate a drop
on run
    open {} & (choose file "Select a file")
end run
-- process files dropped or selected
on open theseItems
    set numberOfItemsRenamed to 0
    set numberOfItemsIgnored to 0
    set numberOfItemsWithoutMetaData to 0

    processItems(theseItems)
    reportResults()
end open

on reportResults()
    -- build the output string
    set output to "Renamed " & numberOfItemsRenamed & " file(s)."
    if numberOfItemsIgnored > 0 then
        set output to output & "
Ignored " & numberOfItemsIgnored & " file(s) with unrecognized filetype."
    end if
    if numberOfItemsWithoutMetaData > 0 then
        set output to output & "
Skipped " & numberOfItemsWithoutMetaData & " file(s) with insufficient TV metadata."
    end if
    display alert output
end reportResults

-- processItems takes a list of items and passes them one at a time to processItem()
on processItems(theseItems)
    repeat with i from 1 to the count of theseItems
        set thisItem to item i of theseItems
        processItem(thisItem)
    end repeat
end processItems

-- processFolder takes a folder and passes the list of contents to processItems()
on processFolder(thisFolder)
    set theseItems to list folder thisFolder without invisibles
    -- processFolder has its own loop due to different syntax for fetching aliases from within a folder
    repeat with i from 1 to the count of theseItems
        set thisItem to alias ((thisFolder as Unicode text) & (item i of theseItems))
        processItem(thisItem)
    end repeat
end processFolder

-- processItem takes an item and passes it to the appropriate next level
-- (processFolder for folders, processFileAlias for approved files, or nothing for non-whitelisted files)
on processItem(thisItem)
    set the item_info to info for thisItem
    -- if a folder was passed, process it as a folder
    if folder of the item_info is true then
        processFolder(thisItem)
    else
        -- fetch the extension, filetype, and typeid to compare to whitelists
        try
            set this_extension to the name extension of item_info
        on error
            set this_extension to ""
        end try
        try
            set this_filetype to the file type of item_info
        on error
            set this_filetype to ""
        end try
        try
            set this_typeID to the type identifier of item_info
        on error
            set this_typeID to ""
        end try
        -- only operate on files, not folders or aliases
        if (folder of the item_info is false) and (alias of the item_info is false) then
            -- only operate on files that conform to whichever whitelists are in use
            set isTypeOk to ((not whitelistTypes) or (this_filetype is in the typeList))
            set isExtensionOk to ((not whitelistExtensions) or (this_extension is in the extensionList))
            set isTypeIDOk to ((not whitelistTypeIDs) or (this_typeID is in the typeIDsList))
            if (isTypeOk and isExtensionOk and isTypeIDOk) then
                -- everything's good so process the file
                processFileAlias(thisItem)
            else
                set numberOfItemsIgnored to numberOfItemsIgnored + 1
            end if
        end if
    end if
end processItem

-- processFileAlias takes a fileAlias and rename the file as a TV episode
on processFileAlias(fileAlias)
    set originalInfo to info for fileAlias
    -- get the file extension (to use when renaming, later)
    set originalName to stripExtension(the name of originalInfo)
    try
        set originalExtension to the name extension of originalInfo
    on error
        set originalExtension to ""
    end try
    -- prep slots for the desired metadata
    set seasonNumber to ""
    set episodeNumber to ""
    set showName to ""
    set episodeName to ""
    -- use exifTool to get the desired metadata
    set theLocation to POSIX path of (fileAlias as text)
    set exifToolOutput to (do shell script pathToExifTool & " -TVShow -TVSeason -TVEpisode -Title " & quoted form of theLocation)
    -- break exifTool output into lines
    set oldTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to return
    set exifToolOutputList to every text item of exifToolOutput
    -- now extract tags from each line
    set AppleScript's text item delimiters to ": "
    set tagList to {}
    repeat with eachItem in exifToolOutputList
        set itemName to trim(true, text item 1 of eachItem)
        -- use items 2-end in case there are :s in the value
        set itemValue to (text items 2 thru end of eachItem) as text
        if itemName = "TV Episode" then
            set episodeNumber to itemValue
        else if itemName = "TV Season" then
            set seasonNumber to itemValue
        else if itemName = "TV Show" then
            set showName to itemValue
        else if itemName = "Title" then
            set episodeName to itemValue
        end if
    end repeat
    set isAllDataPresent to (episodeNumber is not "" and seasonNumber is not "" and showName is not "")
    if isAllDataPresent then
        -- pad the numbers so they alphabetize nicely
        set seasonNumber to padNumberWithZeros(seasonNumber, 2)
        set episodeNumber to padNumberWithZeros(episodeNumber, 2)
        -- build the file name
        set newFileName to showName & " S" & seasonNumber & "E" & episodeNumber & " " & episodeName & "." & originalExtension
        -- remove illegal characters from name
        set newFileName to replaceChars(newFileName, ":", "_")
        set newFileName to replaceChars(newFileName, "/", "_")
        set newFileName to replaceChars(newFileName, "\", "_")
        -- rename the file
        tell application "Finder"
            set name of fileAlias to newFileName
        end tell
        set numberOfItemsRenamed to numberOfItemsRenamed + 1
    else
        set numberOfItemsWithoutMetaData to numberOfItemsWithoutMetaData + 1
    end if
end processFileAlias






(*
Everything after this is utility extensions for dealing with text
*)

on stripExtension(this_name)
    if this_name contains "." then
        set this_name to ¬
            (the reverse of every character of this_name) as string
        set x to the offset of "." in this_name
        set this_name to (text (x + 1) thru -1 of this_name)
        set this_name to (the reverse of every character of this_name) as string
    end if
    return this_name
end stripExtension

on trim(theseCharacters, someText)
    -- Lazy default (AppleScript doesn't support default values)
    if theseCharacters is true then set theseCharacters to ¬
        {" ", tab, ASCII character 10, return, ASCII character 0}

    repeat until first character of someText is not in theseCharacters
        set someText to text 2 thru -1 of someText
    end repeat

    repeat until last character of someText is not in theseCharacters
        set someText to text 1 thru -2 of someText
    end repeat

    return someText
end trim

on replaceChars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replaceChars

on padNumberWithZeros(thisNumber, minLength)
    return text (minLength * -1) thru -1 of ("00000000000000000" & thisNumber)
end padNumberWithZeros
    
respondido por el Robert 12.07.2018 - 04:13

Lea otras preguntas en las etiquetas