Python脚本以将Plex中的所有电影和节目的字幕设置为英语非强制字幕。

huangapple go评论98阅读模式
英文:

Python script to set all subtitles for movies and shows in plex to English non forced subtitles

问题

这段代码已经可以运行并完成。感谢所有帮助过我的人!请随意将此代码用于您自己的目的。我将定期在我的家用服务器上运行此代码以设置首选字幕。干杯!

此代码是在ChatGPT Open AI的帮助下创建的,并由我进一步编辑和完善。它使用Plex Python Api。它将默认将您本地 Plex 媒体库中的所有电影和节目的字幕设置为非强制英文字幕。字幕选择将应用于您的 Plex 配置文件,并在其他设备上记住。假设您的 Plex 字幕设置已在服务器设置中配置,当特定项目的强制字幕可用时,Plex 将默认为强制字幕。这也是为什么创建了此脚本。

请查看下面的答案以获取代码。

也发布在:

英文:

Edit: This code is working and completed. Thanks to all who helped! Feel free to use this code for your own purposes. I will be running this code periodically on my home server to set preferred subtitles. Cheers!

This code was created with the help of ChatGPT Open AI and further edited and completed by me. It uses Plex Python Api. It will set all movies and shows in your local Plex library to English non forced subtitles by default. The subtitle selections will apply to your Plex profile and be remembered on other devices. Assuming your Plex subtitles settings are setup in your server settings Plex will default to Forced Subtitles by default when they are available for a given item. Plex will not allow you to prefer non forced subtitles natively hence why this script was created.

See answer below for the code.

Also posted on:

答案1

得分: 0

以下是Python代码的中文翻译部分:

from plexapi.server import PlexServer
from plexapi.media import SubtitleStream
import os

def main():
    
    # 连接到 Plex 媒体服务器。请替换下面的 PLEX_TOKEN 为您的 Plex 令牌。如何获取令牌:https://www.plexopedia.com/plex-media-server/general/plex-token/
    baseurl = 'http://localhost:32400'
    token = 'PLEX_TOKEN'
    
    script_dir = os.path.dirname(os.path.abspath(__file__))
    token_file = os.path.join(script_dir, 'token.txt')
    
    try:
        with open(token_file, 'r') as f:
            token = f.read().strip()
    except FileNotFoundError:
        pass

    if token == 'PLEX_TOKEN':
        print('\n如何获取您的 Plex 令牌:https://www.plexopedia.com/plex-media-server/general/plex-token/')
        token = input("输入您的 Plex 令牌:")
        with open(token_file, 'w') as f:
            f.write(token)

    plex = PlexServer(baseurl, token)

    table_headers = ['标题', '年份', '状态', '更改']
    title_width = 70
    year_width = 5
    status_width = 20
    changes_width = 8

    print("\n" + "-" * 114 + "\n电影\n" + "-" * 114)
    print(f'\033[1m\033[96m{" | ".join([h.ljust(title_width if i == 0 else year_width if i == 1 else status_width if i == 2 else changes_width) for i, h in enumerate(table_headers)])}\033[0m')

    for section in plex.library.sections():
        if section.type == 'movie':
            for movie in section.all():
                movie.reload()
                english_subs = [stream for stream in movie.subtitleStreams() if stream.languageCode == 'eng']
                non_forced_english_subs = [stream for stream in english_subs if not stream.forced or (hasattr(stream, 'title') and stream.title is not None and 'forced' not in stream.title.lower())]
                forced_english_subs = [stream for stream in english_subs if stream.forced or (hasattr(stream, 'title') and stream.title is not None and 'forced' in stream.title.lower())]
                part = movie.media[0].parts[0]
                partsid = part.id
                if forced_english_subs and non_forced_english_subs:
                    non_forced_english_subs[0].setDefault()
                    print(f'\033[92m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"英语 (非强制)".ljust(status_width)} | {"是".ljust(changes_width)}\033[0m')
                elif non_forced_english_subs and not forced_english_subs:
                    print(f'{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"英语".ljust(status_width)} | {"否".ljust(changes_width)}')
                elif not non_forced_english_subs and not forced_english_subs:
                    print(f'\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"未找到字幕".ljust(status_width)} | {"否".ljust(changes_width)}\033[0m')
                else:
                    print(f'\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"英语 (强制)".ljust(status_width)} | {"否 (错误)".ljust(changes_width)}\033[0m')


    table_headers = ['标题', '年份', '季数', '剧集编号', '状态', '更改']
    title_width = 42
    year_width = 5
    season_width = 11
    episode_width = 11
    status_width = 20
    changes_width = 8
    season_row_width = 4
    episode_row_width = 3

    print("\n" + "-" * 114 + "\n电视节目\n" + "-" * 114)
    print(f'\033[1m\033[96m{" | ".join([h.ljust(title_width if i == 0 else year_width if i == 1 else season_width if i == 2 else episode_width if i == 3 else status_width if i == 4 else changes_width) for i, h in enumerate(table_headers)])}\033[0m')

    for section in plex.library.sections():
        if section.type == 'show':
            for show in section.all():
                show.reload()
                for episode in show.episodes():
                    show.reload()
                    episode.reload()
                    english_subs = [stream for stream in episode.subtitleStreams() if stream.languageCode == 'eng']
                    non_forced_english_subs = [stream for stream in english_subs if not stream.forced or (hasattr(stream, 'title') and stream.title is not None and 'forced' not in stream.title.lower())]
                    forced_english_subs = [stream for stream in english_subs if stream.forced or (hasattr(stream, 'title') and stream.title is not None and 'forced' in stream.title.lower())]
                    part = episode.media[0].parts[0]
                    partsid = part.id
                    if forced_english_subs and non_forced_english_subs:
                        non_forced_english_subs[0].setDefault()
                        print(f'\033[92m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"第" + str(episode.seasonNumber).ljust(season_row_width) + "季"} | {"第" + str(episode.index).ljust(episode_row_width) + "集"} | {"英语 (非强制)".ljust(status_width)} | {"是".ljust(changes_width)}\033[0m')
                    elif non_forced_english_subs and not forced_english_subs:
                        print(f'{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"第" + str(episode.seasonNumber).ljust(season_row_width) + "季"} | {"第" + str(episode.index).ljust(episode_row_width) + "集"} | {"英语".ljust(status_width)} | {"否".ljust(changes_width)}')
                    elif not non_forced_english_subs and not forced_english_subs and not forced_english_subs:
                        print(f'\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"第" + str(episode.seasonNumber).ljust(season_row_width) + "季"} | {"第" + str(episode.index).ljust(episode_row

<details>
<summary>英文:</summary>

Python code:

    from plexapi.server import PlexServer
    from plexapi.media import SubtitleStream
    import os
    
    def main():
        
        # Connect to Plex Media Server. Replace PLEX_TOKEN below with your Plex token. How to get token: https://www.plexopedia.com/plex-media-server/general/plex-token/
        baseurl = &#39;http://localhost:32400&#39;
        token = &#39;PLEX_TOKEN&#39;
        
        script_dir = os.path.dirname(os.path.abspath(__file__))
        token_file = os.path.join(script_dir, &#39;token.txt&#39;)
        
        try:
            with open(token_file, &#39;r&#39;) as f:
                token = f.read().strip()
        except FileNotFoundError:
            pass
    
        if token == &#39;PLEX_TOKEN&#39;:
            print(f&#39;\nHow to get your Plex token: https://www.plexopedia.com/plex-media-server/general/plex-token/\n&#39;)
            token = input(&quot;Enter your Plex token: &quot;)
            with open(token_file, &#39;w&#39;) as f:
                f.write(token)
    
        plex = PlexServer(baseurl, token)
    
        table_headers = [&#39;Title&#39;, &#39;Year&#39;, &#39;Status&#39;, &#39;Changes&#39;]
        title_width = 70
        year_width = 5
        status_width = 20
        changes_width = 8
    
        print(&quot;\n&quot; + &quot;-&quot; * 114 + &quot;\nMovies\n&quot; + &quot;-&quot; * 114)
        print(f&#39;\033[1m\033[96m{&quot; | &quot;.join([h.ljust(title_width if i == 0 else year_width if i == 1 else status_width if i == 2 else changes_width) for i, h in enumerate(table_headers)])}\033[0m&#39;)
    
        for section in plex.library.sections():
            if section.type == &#39;movie&#39;:
                for movie in section.all():
                    movie.reload()
                    english_subs = [stream for stream in movie.subtitleStreams() if stream.languageCode == &#39;eng&#39;]
                    non_forced_english_subs = [stream for stream in english_subs if not stream.forced or (hasattr(stream, &#39;title&#39;) and stream.title is not None and &#39;forced&#39; not in stream.title.lower())]
                    forced_english_subs = [stream for stream in english_subs if stream.forced or (hasattr(stream, &#39;title&#39;) and stream.title is not None and &#39;forced&#39; in stream.title.lower())]
                    part = movie.media[0].parts[0]
                    partsid = part.id
                    if forced_english_subs and non_forced_english_subs:
                        non_forced_english_subs[0].setDefault()
                        print(f&#39;\033[92m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {&quot;English (Non-Forced)&quot;.ljust(status_width)} | {&quot;Y&quot;.ljust(changes_width)}\033[0m&#39;)
                    elif non_forced_english_subs and not forced_english_subs:
                        print(f&#39;{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {&quot;English&quot;.ljust(status_width)} | {&quot;N&quot;.ljust(changes_width)}&#39;)
                    elif not non_forced_english_subs and not forced_english_subs:
                        print(f&#39;\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {&quot;No Subtitles Found&quot;.ljust(status_width)} | {&quot;N&quot;.ljust(changes_width)}\033[0m&#39;)
                    else:
                        print(f&#39;\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {&quot;English (Forced)&quot;.ljust(status_width)} | {&quot;N (Error)&quot;.ljust(changes_width)}\033[0m&#39;)
    
    
        table_headers = [&#39;Title&#39;, &#39;Year&#39;, &#39;Season #&#39;, &#39;Episode #&#39;, &#39;Status&#39;, &#39;Changes&#39;]
        title_width = 42
        year_width = 5
        season_width = 11
        episode_width = 11
        status_width = 20
        changes_width = 8
        season_row_width = 4
        episode_row_width = 3
    
        print(&quot;\n&quot; + &quot;-&quot; * 114 + &quot;\nShows\n&quot; + &quot;-&quot; * 114)
        print(f&#39;\033[1m\033[96m{&quot; | &quot;.join([h.ljust(title_width if i == 0 else year_width if i == 1 else season_width if i == 2 else episode_width if i == 3 else status_width if i == 4 else changes_width) for i, h in enumerate(table_headers)])}\033[0m&#39;)
    
        for section in plex.library.sections():
            if section.type == &#39;show&#39;:
                for show in section.all():
                    show.reload()
                    for episode in show.episodes():
                        show.reload()
                        episode.reload()
                        english_subs = [stream for stream in episode.subtitleStreams() if stream.languageCode == &#39;eng&#39;]
                        non_forced_english_subs = [stream for stream in english_subs if not stream.forced or (hasattr(stream, &#39;title&#39;) and stream.title is not None and &#39;forced&#39; not in stream.title.lower())]
                        forced_english_subs = [stream for stream in english_subs if stream.forced or (hasattr(stream, &#39;title&#39;) and stream.title is not None and &#39;forced&#39; in stream.title.lower())]
                        part = episode.media[0].parts[0]
                        partsid = part.id
                        if forced_english_subs and non_forced_english_subs:
                            non_forced_english_subs[0].setDefault()
                            print(f&#39;\033[92m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {&quot;Season &quot; + str(episode.seasonNumber).ljust(season_row_width)} | {&quot;Episode &quot; + str(episode.index).ljust(episode_row_width)} | {&quot;English (Non-Forced)&quot;.ljust(status_width)} | {&quot;Y&quot;.ljust(changes_width)}\033[0m&#39;)
                        elif non_forced_english_subs and not forced_english_subs:
                            print(f&#39;{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {&quot;Season &quot; + str(episode.seasonNumber).ljust(season_row_width)} | {&quot;Episode &quot; + str(episode.index).ljust(episode_row_width)} | {&quot;English&quot;.ljust(status_width)} | {&quot;N&quot;.ljust(changes_width)}&#39;)
                        elif not non_forced_english_subs and not forced_english_subs and not forced_english_subs:
                            print(f&#39;\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {&quot;Season &quot; + str(episode.seasonNumber).ljust(season_row_width)} | {&quot;Episode &quot; + str(episode.index).ljust(episode_row_width)} | {&quot;No Subtitles Found&quot;.ljust(status_width)} | {&quot;N&quot;.ljust(changes_width)}\033[0m&#39;)
                        else:
                            print(f&#39;\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {&quot;Season &quot; + str(episode.seasonNumber).ljust(season_row_width)} | {&quot;Episode &quot; + str(episode.index).ljust(episode_row_width)} | {&quot;English (Forced)&quot;.ljust(status_width)} | {&quot;N (Error)&quot;.ljust(changes_width)}\033[0m&#39;)
    
    
    if __name__ == &#39;__main__&#39;:
        main()

</details>



huangapple
  • 本文由 发表于 2023年1月6日 15:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定