{"id":257,"date":"2026-06-21T19:51:50","date_gmt":"2026-06-21T11:51:50","guid":{"rendered":"https:\/\/www.isunding.com\/?p=257"},"modified":"2026-06-21T20:12:19","modified_gmt":"2026-06-21T12:12:19","slug":"deepseek-%e5%ad%97%e5%b9%95%e7%bf%bb%e8%af%91%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/www.isunding.com\/?p=257","title":{"rendered":"Deepseek \u5b57\u5e55\u7ffb\u8bd1\u811a\u672c"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5b57\u5e55\u6279\u91cf\u7ffb\u8bd1\u5de5\u5177\uff08DeepSeek API \u79bb\u7ebf SRT \u7ffb\u8bd1\u811a\u672c\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u529f\u80fd\u6982\u8ff0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u57fa\u4e8e Python + DeepSeek \u4e91\u7aef API \u5f00\u53d1\u7684\u672c\u5730 SRT \u5b57\u5e55\u6279\u91cf\u7ffb\u8bd1\u5de5\u5177\uff0c\u8fd0\u884c\u540e\u5f39\u7a97\u9009\u62e9\u5b57\u5e55\u6587\u4ef6\u5939\uff0c\u81ea\u52a8\u6279\u91cf\u7ffb\u8bd1\u76ee\u5f55\u5185\u6240\u6709\u539f\u7248<code>.srt<\/code>\u5b57\u5e55\uff1b\u77ed\u53f0\u8bcd\u3001\u8bed\u6c14\u52a9\u8bcd\u3001\u7eaf\u6807\u70b9 \/ \u6570\u5b57\u77ed\u53e5\u81ea\u52a8\u8df3\u8fc7\u7ffb\u8bd1\uff0c\u51cf\u5c11 API \u8c03\u7528\u6d88\u8017\uff0c\u540c\u65f6\u89e3\u51b3 AI \u7ffb\u8bd1\u5e8f\u53f7\u9519\u4e71\u3001\u6f0f\u8bd1\u3001\u63a5\u53e3\u8d85\u65f6\u4e2d\u65ad\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6838\u5fc3\u7279\u6027<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u53ef\u89c6\u5316\u6587\u4ef6\u5939\u9009\u62e9<\/strong>\uff1a\u5185\u7f6e\u5f39\u7a97\u9009\u53d6\u5b57\u5e55\u76ee\u5f55\uff0c\u65e0\u9700\u624b\u52a8\u4fee\u6539\u4ee3\u7801\u8def\u5f84\uff1b<\/li>\n\n\n\n<li><strong>\u667a\u80fd\u77ed\u53e5\u8fc7\u6ee4<\/strong>\uff1a\u5b57\u7b26\u5c11\u4e8e\u6307\u5b9a\u957f\u5ea6\u3001\u7eaf\u6807\u70b9\u3001\u7eaf\u6570\u5b57\u5b57\u5e55\u76f4\u63a5\u4fdd\u7559\u539f\u6587\uff0c\u4e0d\u53d1\u8d77\u7ffb\u8bd1\u8bf7\u6c42\uff0c\u8282\u7701 Token \u4e0e\u7ffb\u8bd1\u65f6\u957f\uff1b<\/li>\n\n\n\n<li><strong>\u7a33\u5065\u7ffb\u8bd1\u5bb9\u9519\u673a\u5236<\/strong>\uff1aAPI \u8bf7\u6c42\u5931\u8d25\u81ea\u52a8\u91cd\u8bd5\uff0c\u7f51\u7edc\u5f02\u5e38\u6279\u6b21\u4fdd\u7559\u539f\u59cb\u5b57\u5e55\u4e0d\u4e22\u5931\uff1b\u72ec\u7acb\u5e8f\u53f7\u6620\u5c04\u89e3\u6790\u8bd1\u6587\uff0c\u675c\u7edd\u591a\u884c\u53f0\u8bcd\u3001\u6362\u884c\u5bfc\u81f4\u7684\u5b57\u5e55\u5339\u914d\u9519\u4e71\u3001\u6f0f\u7ffb\uff1b<\/li>\n\n\n\n<li><strong>\u6587\u4ef6\u8f93\u51fa\u89c4\u5219<\/strong>\uff1a\u7ffb\u8bd1\u5b8c\u6210\u6587\u4ef6\u4e0e\u539f\u5b57\u5e55\u5b58\u653e\u4e8e\u540c\u4e00\u6587\u4ef6\u5939\uff0c\u81ea\u52a8\u547d\u540d\u4e3a <code>xxx.chn.srt<\/code>\uff1b\u81ea\u52a8\u8fc7\u6ee4\u5df2\u751f\u6210\u7684<code>.chn.srt<\/code>\u6587\u4ef6\uff0c\u907f\u514d\u91cd\u590d\u7ffb\u8bd1\uff1b<\/li>\n\n\n\n<li><strong>\u9650\u6d41\u9632\u62a4<\/strong>\uff1a\u6bcf\u6279\u6b21\u7ffb\u8bd1\u6dfb\u52a0\u8bf7\u6c42\u95f4\u9694\uff0c\u9632\u6b62 API \u77ed\u65f6\u95f4\u9ad8\u9891\u8c03\u7528\u88ab\u9650\u6d41\uff1b<\/li>\n\n\n\n<li><strong>\u53ef\u9009\u53cc\u8bed\u5b57\u5e55<\/strong>\uff1a\u652f\u6301\u4e00\u952e\u5207\u6362\u300c\u4ec5\u4e2d\u6587\u8bd1\u6587\u300d\u300c\u539f\u6587 + \u4e2d\u6587\u53cc\u8bed\u5bf9\u7167\u300d\u4e24\u79cd\u5b57\u5e55\u8f93\u51fa\u683c\u5f0f\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u6d41\u7a0b<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u914d\u7f6e DeepSeek API \u5bc6\u94a5\u3001\u6279\u91cf\u6761\u6570\u3001\u77ed\u53e5\u8fc7\u6ee4\u9608\u503c\u7b49\u53c2\u6570\uff1b<\/li>\n\n\n\n<li>\u8fd0\u884c\u811a\u672c\uff0c\u5f39\u7a97\u9009\u4e2d\u5b58\u653e SRT \u5b57\u5e55\u7684\u6587\u4ef6\u5939\uff1b<\/li>\n\n\n\n<li>\u7a0b\u5e8f\u81ea\u52a8\u7b5b\u9009\u539f\u7248\u5b57\u5e55\uff0c\u5206\u7247\u8c03\u7528 DeepSeek \u63a5\u53e3\u7ffb\u8bd1\uff1b<\/li>\n\n\n\n<li>\u540c\u76ee\u5f55\u751f\u6210\u5e26<code>.chn<\/code>\u540e\u7f00\u7684\u4e2d\u6587\u5b57\u5e55\u6587\u4ef6\uff0c\u5168\u7a0b\u63a7\u5236\u53f0\u6253\u5370\u5904\u7406\u8fdb\u5ea6\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f9d\u8d56\u73af\u5883<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python3.12\uff0c\u9700\u63d0\u524d\u5b89\u88c5 <code>openai<\/code>\u3001<code>srt<\/code> \u4f9d\u8d56\u5e93\uff1b\u7f51\u7edc\u53ef\u8bbf\u95ee DeepSeek API\u3001\u8d26\u6237\u62e5\u6709\u53ef\u7528\u4f59\u989d\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u6e90\u4ee3\u7801<\/summary>\n<pre class=\"wp-block-code has-small-font-size\" style=\"font-style:normal;font-weight:300\"><code>from openai import OpenAI\nimport srt\nimport os\nimport tkinter as tk\nfrom tkinter import filedialog\nimport time\nimport string\n\n# ===================== \u3010\u914d\u7f6e\u533a\u81ea\u884c\u4fee\u6539\u3011 =====================\nDEEPSEEK_API_KEY = \"sk-\u66ff\u6362\u6210\u4f60\u7684\u5bc6\u94a5\"\nBATCH_SIZE = 5       # \u8c03\u5c0f\uff0c\u964d\u4f4e\u4e22\u53e5\u6982\u7387\nMODEL_NAME = \"deepseek-v4-flash\"\nREQUEST_INTERVAL = 0.8  # \u6bcf\u6279\u8bf7\u6c42\u95f4\u9694\uff0c\u9632API\u9650\u6d41\nMAX_RETRY = 2        # \u63a5\u53e3\u5931\u8d25\u91cd\u8bd5\u6b21\u6570\nSKIP_MAX_CHAR = 4    # \u5c11\u4e8e\u7b49\u4e8e\u8be5\u5b57\u7b26\u76f4\u63a5\u8df3\u8fc7\u7ffb\u8bd1\nSUFFIX_TAG = \".chn\"  # \u7ffb\u8bd1\u6587\u4ef6\u540e\u7f00\u6807\u8bc6\n# ==========================================================\n\nclient = OpenAI(\n    api_key=DEEPSEEK_API_KEY,\n    base_url=\"https:\/\/api.deepseek.com\"\n)\n\ndef select_folder_dialog():\n    root = tk.Tk()\n    root.withdraw()\n    folder_path = filedialog.askdirectory(title=\"\u8bf7\u9009\u62e9\u5b58\u653eSRT\u5b57\u5e55\u7684\u6e90\u6587\u4ef6\u5939\")\n    return folder_path\n\ndef is_skip_text(text: str) -> bool:\n    \"\"\"\u5224\u65ad\u662f\u5426\u4e3a\u77ed\u53e5\/\u8bed\u6c14\u8bcd\/\u7eaf\u7b26\u53f7\uff0c\u9700\u8981\u8df3\u8fc7\u7ffb\u8bd1\"\"\"\n    txt = text.strip()\n    if len(txt) &lt;= SKIP_MAX_CHAR:\n        return True\n    # \u5168\u662f\u6807\u70b9\u7b26\u53f7\n    all_punc = all(c in string.punctuation + \" \" for c in txt)\n    if all_punc:\n        return True\n    # \u7eaf\u6570\u5b57\n    if txt.isdigit():\n        return True\n    return False\n\ndef batch_translate(sub_batch):\n    # \u62c6\u5206\uff1a\u9700\u8981\u7ffb\u8bd1\u7684\u6761\u76ee \/ \u76f4\u63a5\u4fdd\u7559\u539f\u6587\u7684\u6761\u76ee\n    need_trans = &#91;]\n    keep_origin = &#91;]\n    for idx, sub in enumerate(sub_batch):\n        if is_skip_text(sub.content):\n            keep_origin.append((idx, sub))\n        else:\n            need_trans.append((idx, sub))\n\n    # \u6ca1\u6709\u9700\u8981\u7ffb\u8bd1\u7684\uff0c\u76f4\u63a5\u8fd4\u56de\u539f\u6279\u6b21\n    if not need_trans:\n        time.sleep(REQUEST_INTERVAL)\n        return sub_batch\n\n    # \u7ec4\u88c5\u5f85\u7ffb\u8bd1\u6587\u672c\n    send_text = \"\"\n    map_idx = {}  # \u8bb0\u5f55\u5185\u90e8\u5e8f\u53f7\u5bf9\u5e94\u539f\u59cb\u4e0b\u6807\n    for inner_id, (ori_idx, sub) in enumerate(need_trans):\n        map_idx&#91;inner_id + 1] = ori_idx\n        send_text += f\"{inner_id+1}\\n{sub.content.strip()}\\n\\n\"\n\n    retry = 0\n    raw_result = \"\"\n    while retry &lt;= MAX_RETRY:\n        try:\n            resp = client.chat.completions.create(\n                model=MODEL_NAME,\n                messages=&#91;\n                    {\n                        \"role\": \"system\",\n                        \"content\": \"\"\"\u4f60\u662f\u4e13\u4e1a\u5f71\u89c6\u5b57\u5e55\u7ffb\u8bd1\u3002\n\u8f93\u5165\u662f\u5e26\u6570\u5b57\u5e8f\u53f7\u7684\u53f0\u8bcd\uff0c\u4e25\u683c\u6309\u987a\u5e8f\u9010\u6761\u7ffb\u8bd1\u4e3a\u901a\u987a\u7b80\u4f53\u4e2d\u6587\u3002\n\u8f93\u51fa\u683c\u5f0f\uff1a\u6bcf\u6761\u8bd1\u6587\u524d\u5e26\u4e0a\u5bf9\u5e94\u6570\u5b57\uff0c\u4e00\u6761\u5e8f\u53f7\u5bf9\u5e94\u4e00\u53e5\u8bd1\u6587\uff0c\u4e0d\u8981\u5408\u5e76\u3001\u4e0d\u8981\u6f0f\u8bd1\u3002\n\u53ea\u8f93\u51fa\u8bd1\u6587\u5185\u5bb9\uff0c\u4e0d\u52a0\u4efb\u4f55\u89e3\u91ca\u3001\u6ce8\u91ca\u3001\u591a\u4f59\u6587\u5b57\u3002\"\"\"\n                    },\n                    {\"role\": \"user\", \"content\": f\"\u7ffb\u8bd1\u4ee5\u4e0b\u53f0\u8bcd\uff1a\\n{send_text}\"}\n                ],\n                temperature=0.2,\n                timeout=15\n            )\n            raw_result = resp.choices&#91;0].message.content.strip()\n            break\n        except Exception as e:\n            retry += 1\n            print(f\"\u6279\u6b21\u8bf7\u6c42\u5931\u8d25\uff0c\u91cd\u8bd5{retry}\/{MAX_RETRY}\uff0c\u9519\u8bef\uff1a{str(e)}\")\n            time.sleep(1.2)\n            if retry > MAX_RETRY:\n                # \u591a\u6b21\u5931\u8d25\u4fdd\u7559\u539f\u6587\n                time.sleep(REQUEST_INTERVAL)\n                return sub_batch\n\n    # \u89e3\u6790\u8bd1\u6587\n    trans_map = {}\n    current_num = None\n    current_text = &#91;]\n    for line in raw_result.splitlines():\n        line = line.strip()\n        if not line:\n            continue\n        if line.isdigit():\n            if current_num is not None and current_text:\n                trans_map&#91;current_num] = \" \".join(current_text)\n            current_num = int(line)\n            current_text = &#91;]\n        else:\n            current_text.append(line)\n    if current_num is not None and current_text:\n        trans_map&#91;current_num] = \" \".join(current_text)\n\n    # \u56de\u586b\u7ffb\u8bd1\n    for inner_seq, ori_idx in map_idx.items():\n        if inner_seq in trans_map and trans_map&#91;inner_seq].strip():\n            sub_batch&#91;ori_idx].content = trans_map&#91;inner_seq]\n            # \u53cc\u8bed\u5f00\u542f\u66ff\u6362\u4e0a\u9762\u8fd9\u884c\u4e3a\uff1a\n            # sub_batch&#91;ori_idx].content = sub_batch&#91;ori_idx].content + \"\\n\u3010\u8bd1\u3011\" + trans_map&#91;inner_seq]\n\n    time.sleep(REQUEST_INTERVAL)\n    return sub_batch\n\ndef process_single_srt(src_dir, filename):\n    src_path = os.path.join(src_dir, filename)\n    # \u6784\u9020\u8f93\u51fa\u6587\u4ef6\u540d\uff1atest.srt \u2192 test.chn.srt\n    name_no_ext, ext = os.path.splitext(filename)\n    out_filename = f\"{name_no_ext}{SUFFIX_TAG}{ext}\"\n    out_path = os.path.join(src_dir, out_filename)\n\n    print(f\"\\n==== \u6b63\u5728\u5904\u7406\uff1a{filename} \u2192 {out_filename} ====\")\n    try:\n        with open(src_path, \"r\", encoding=\"utf-8\") as f:\n            subs = list(srt.parse(f.read()))\n    except Exception as e:\n        print(f\"\u8bfb\u53d6\u5b57\u5e55\u5931\u8d25\uff1a{e}\")\n        return\n\n    total = len(subs)\n    for start in range(0, total, BATCH_SIZE):\n        end = start + BATCH_SIZE\n        chunk = subs&#91;start:end]\n        chunk = batch_translate(chunk)\n        subs&#91;start:end] = chunk\n        print(f\"\u8fdb\u5ea6\uff1a{min(end, total)}\/{total}\")\n\n    # \u4fdd\u5b58\u7ffb\u8bd1\u540e\u5b57\u5e55\u5230\u6e90\u6587\u4ef6\u5939\n    with open(out_path, \"w\", encoding=\"utf-8\") as f:\n        f.write(srt.compose(subs))\n    print(f\"\u6587\u4ef6\u5b8c\u6210\u8f93\u51fa\uff1a{out_path}\")\n\ndef main():\n    input_dir = select_folder_dialog()\n    if not input_dir:\n        print(\"\u672a\u9009\u62e9\u6587\u4ef6\u5939\uff0c\u7a0b\u5e8f\u9000\u51fa\")\n        return\n\n    print(f\"\u6e90\u76ee\u5f55\uff1a{input_dir}\")\n    print(f\"\u8fc7\u6ee4\u89c4\u5219\uff1a\u2264{SKIP_MAX_CHAR}\u5b57\u7b26\u3001\u7eaf\u6807\u70b9\u3001\u7eaf\u6570\u5b57\u81ea\u52a8\u8df3\u8fc7\u7ffb\u8bd1\")\n    print(f\"\u8f93\u51fa\u89c4\u5219\uff1a\u540c\u76ee\u5f55\u751f\u6210 *.chn.srt \u7ffb\u8bd1\u5b57\u5e55\\n\")\n\n    all_files = os.listdir(input_dir)\n    srt_files = &#91;f for f in all_files if f.lower().endswith(\".srt\") and SUFFIX_TAG not in f]\n    if not srt_files:\n        print(\"\u6587\u4ef6\u5939\u5185\u672a\u627e\u5230\u672a\u7ffb\u8bd1\u7684SRT\u5b57\u5e55\")\n        return\n\n    print(f\"\u5171\u68c0\u6d4b\u5230 {len(srt_files)} \u4e2a\u5b57\u5e55\u6587\u4ef6\uff0c\u5f00\u59cb\u7ffb\u8bd1\")\n    for filename in srt_files:\n        process_single_srt(input_dir, filename)\n\n    print(\"\\n==================== \u5168\u90e8\u4efb\u52a1\u7ed3\u675f ====================\")\n    print(f\"\u7ffb\u8bd1\u5b8c\u6210\u7684 .chn.srt \u6587\u4ef6\u4fdd\u5b58\u5728\u539f\u5b57\u5e55\u540c\u4e00\u76ee\u5f55\")\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n<\/details>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u5e55\u6279\u91cf\u7ffb\u8bd1\u5de5\u5177\uff08DeepSeek API \u79bb\u7ebf S&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[34],"tags":[21,24],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-34","tag-windows","tag-24"],"_links":{"self":[{"href":"https:\/\/www.isunding.com\/index.php?rest_route=\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.isunding.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.isunding.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.isunding.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.isunding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=257"}],"version-history":[{"count":3,"href":"https:\/\/www.isunding.com\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/www.isunding.com\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions\/262"}],"wp:attachment":[{"href":"https:\/\/www.isunding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.isunding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.isunding.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}