{"id":967210,"date":"2024-12-27T04:54:55","date_gmt":"2024-12-26T20:54:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/967210.html"},"modified":"2024-12-27T04:54:57","modified_gmt":"2024-12-26T20:54:57","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%8a%95%e5%b1%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/967210.html","title":{"rendered":"\u5982\u4f55\u7528python\u6295\u5c4f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182614\/c9301a4c-3f2d-4b21-bc58-65772c36ad22.webp\" alt=\"\u5982\u4f55\u7528python\u6295\u5c4f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8fdb\u884c\u6295\u5c4f\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u5982PyChromecast\u5b9e\u73b0Chromecast\u6295\u5c4f\u3001\u4f7f\u7528OpenCV\u7ed3\u5408\u5176\u4ed6\u5e93\u5b9e\u73b0\u5c4f\u5e55\u6355\u6349\u4e0e\u4f20\u8f93\u3001\u901a\u8fc7DLNA\u534f\u8bae\u5b9e\u73b0\u8de8\u8bbe\u5907\u6295\u5c4f\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5229\u7528PyChromecast\u5b9e\u73b0Chromecast\u6295\u5c4f\u3002<\/strong><\/p>\n<\/p>\n<p><p>PyChromecast\u662f\u4e00\u4e2a\u5141\u8bb8\u60a8\u5c06\u5a92\u4f53\u5185\u5bb9\u4ecePython\u5e94\u7528\u7a0b\u5e8f\u6d41\u5f0f\u4f20\u8f93\u5230Chromecast\u8bbe\u5907\u7684\u5e93\u3002\u5b83\u652f\u6301\u97f3\u9891\u3001\u89c6\u9891\u548c\u56fe\u7247\u7684\u6295\u5c4f\uff0c\u9002\u7528\u4e8e\u60f3\u8981\u5728\u5bb6\u5ead\u7f51\u7edc\u4e2d\u8f7b\u677e\u7ba1\u7406\u591a\u5a92\u4f53\u5185\u5bb9\u7684\u5f00\u53d1\u8005\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PYCHROMECAST\u5e93\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>PyChromecast\u662fPython\u4e2d\u7528\u4e8e\u4e0eGoogle Chromecast\u8bbe\u5907\u4ea4\u4e92\u7684\u5e93\u3002\u5b83\u80fd\u591f\u53d1\u73b0\u7f51\u7edc\u4e0a\u7684Chromecast\u8bbe\u5907\uff0c\u5e76\u5c06\u5a92\u4f53\u6d41\u53d1\u9001\u5230\u8fd9\u4e9b\u8bbe\u5907\u3002\u4e3a\u4e86\u4f7f\u7528\u6b64\u5e93\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u60a8\u7684\u8ba1\u7b97\u673a\u548cChromecast\u8bbe\u5907\u5728\u540c\u4e00\u7f51\u7edc\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5PyChromecast<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5\u4e86PyChromecast\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyChromecast<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5bfc\u5165PyChromecast\u5e93\u5e76\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u53d1\u73b0Chromecast\u8bbe\u5907<\/h4>\n<\/p>\n<p><p>PyChromecast\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u53d1\u73b0\u7f51\u7edc\u4e2d\u7684Chromecast\u8bbe\u5907\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u53d1\u73b0\u548c\u5217\u51fa\u6240\u6709\u53ef\u7528\u8bbe\u5907\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pychromecast<\/p>\n<h2><strong>\u53d1\u73b0\u6240\u6709\u53ef\u7528\u7684Chromecast\u8bbe\u5907<\/strong><\/h2>\n<p>chromecasts, browser = pychromecast.get_listed_chromecasts()<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2a\u8bbe\u5907\u7684\u540d\u79f0<\/strong><\/h2>\n<p>for cc in chromecasts:<\/p>\n<p>    print(f&quot;Found Chromecast: {cc.device.friendly_name}&quot;)<\/p>\n<h2><strong>\u505c\u6b62\u641c\u7d22<\/strong><\/h2>\n<p>pychromecast.discovery.stop_discovery(browser)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PYCHROMECAST\u8fdb\u884c\u6295\u5c4f<\/h3>\n<\/p>\n<p><p>\u5728\u53d1\u73b0\u8bbe\u5907\u540e\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u8981\u6295\u5c4f\u7684\u8bbe\u5907\u5e76\u5f00\u59cb\u5a92\u4f53\u6d41\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8fde\u63a5\u5230Chromecast\u8bbe\u5907<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u8981\u8fde\u63a5\u7684Chromecast\u8bbe\u5907\uff0c\u7136\u540e\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u9009\u62e9\u7b2c\u4e00\u4e2a\u8bbe\u5907<\/p>\n<p>cast = chromecasts[0]<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u8bbe\u5907<\/strong><\/h2>\n<p>cast.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t()<\/p>\n<p>print(f&quot;Connected to Chromecast: {cast.device.friendly_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6295\u5c4f\u5a92\u4f53<\/h4>\n<\/p>\n<p><p>PyChromecast\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u5a92\u4f53\u6295\u5c4f\u3002\u4ee5\u4e0b\u662f\u64ad\u653e\u89c6\u9891\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5a92\u4f53\u63a7\u5236\u5668<\/p>\n<p>mc = cast.media_controller<\/p>\n<h2><strong>\u6295\u5c4f\u4e00\u4e2a\u89c6\u9891URL<\/strong><\/h2>\n<p>video_url = &quot;http:\/\/commondatastorage.googleapis.com\/gtv-videos-bucket\/sample\/BigBuckBunny.mp4&quot;<\/p>\n<p>mc.play_media(video_url, &quot;video\/mp4&quot;)<\/p>\n<h2><strong>\u5f00\u59cb\u64ad\u653e<\/strong><\/h2>\n<p>mc.block_until_active()<\/p>\n<p>print(&quot;Playing video on Chromecast&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539<code>play_media<\/code>\u51fd\u6570\u4e2d\u7684URL\u548c\u5a92\u4f53\u7c7b\u578b\u6765\u6295\u5c4f\u4e0d\u540c\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u63a7\u5236\u64ad\u653e<\/h3>\n<\/p>\n<p><p>\u6295\u5c4f\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5a92\u4f53\u63a7\u5236\u5668\u6765\u63a7\u5236\u64ad\u653e\uff0c\u5982\u6682\u505c\u3001\u7ee7\u7eed\u3001\u505c\u6b62\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6682\u505c\u548c\u7ee7\u7eed\u64ad\u653e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6682\u505c\u64ad\u653e<\/p>\n<p>mc.pause()<\/p>\n<p>print(&quot;Video paused&quot;)<\/p>\n<h2><strong>\u7ee7\u7eed\u64ad\u653e<\/strong><\/h2>\n<p>mc.play()<\/p>\n<p>print(&quot;Video resumed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u505c\u6b62\u64ad\u653e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u505c\u6b62\u64ad\u653e<\/p>\n<p>mc.stop()<\/p>\n<p>print(&quot;Video stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u6295\u5c4f\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86PyChromecast\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7Python\u5b9e\u73b0\u6295\u5c4f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5176\u4ed6\u5e38\u7528\u65b9\u6cd5\u7684\u6982\u8ff0\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528OpenCV\u548c\u5176\u4ed6\u5e93\u8fdb\u884c\u5c4f\u5e55\u6355\u6349\u548c\u4f20\u8f93<\/h4>\n<\/p>\n<p><p>OpenCV\u53ef\u4ee5\u7528\u4e8e\u6355\u6349\u5c4f\u5e55\u5185\u5bb9\uff0c\u7136\u540e\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u5230\u5176\u4ed6\u8bbe\u5907\u4e0a\u8fdb\u884c\u5c55\u793a\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u81ea\u5b9a\u4e49\u5c4f\u5e55\u6355\u6349\u548c\u4f20\u8f93\u903b\u8f91\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u901a\u8fc7DLNA\u534f\u8bae\u5b9e\u73b0\u8de8\u8bbe\u5907\u6295\u5c4f<\/h4>\n<\/p>\n<p><p>DLNA\u662f\u4e00\u79cd\u7528\u4e8e\u5bb6\u5ead\u7f51\u7edc\u4e2d\u591a\u5a92\u4f53\u8bbe\u5907\u4e92\u901a\u7684\u534f\u8bae\u3002Python\u4e2d\u6709\u4e00\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0DLNA\u534f\u8bae\u7684\u6295\u5c4f\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u652f\u6301DLNA\u7684\u8bbe\u5907\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528PyChromecast\u5e93\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u591a\u5a92\u4f53\u5185\u5bb9\u6295\u5c4f\u5230Chromecast\u8bbe\u5907\u4e0a\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528PyChromecast\u5e93\u8fdb\u884c\u8bbe\u5907\u53d1\u73b0\u3001\u8fde\u63a5\u3001\u5a92\u4f53\u6295\u5c4f\u548c\u64ad\u653e\u63a7\u5236\u3002\u6b64\u5916\uff0c\u8fd8\u7b80\u8981\u4ecb\u7ecd\u4e86\u5176\u4ed6\u53ef\u9009\u7684\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528OpenCV\u548cDLNA\u534f\u8bae\u5b9e\u73b0\u6295\u5c4f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u6295\u5c4f\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u6295\u5c4f\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u548c\u5de5\u5177\u5b9e\u73b0\u6295\u5c4f\u529f\u80fd\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>pyautogui<\/code>\u3001<code>opencv<\/code>\u548c<code>socket<\/code>\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u5e93\u6355\u6349\u5c4f\u5e55\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7\u7f51\u7edc\u6216\u5176\u4ed6\u8bbe\u5907\u8fdb\u884c\u6295\u5c4f\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u53ef\u4ee5\u53c2\u8003\u76f8\u5173\u6587\u6863\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<p><strong>\u5728\u6295\u5c4f\u8fc7\u7a0b\u4e2d\uff0cPython\u7684\u6027\u80fd\u4f1a\u53d7\u5230\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><br \/>\u6295\u5c4f\u65f6\uff0cPython\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u591a\u4e2a\u56e0\u7d20\u5f71\u54cd\uff0c\u5305\u62ec\u7f51\u7edc\u5e26\u5bbd\u3001\u5c4f\u5e55\u5206\u8fa8\u7387\u4ee5\u53ca\u56fe\u50cf\u5904\u7406\u7684\u590d\u6742\u5ea6\u3002\u9009\u62e9\u5408\u9002\u7684\u5206\u8fa8\u7387\u548c\u538b\u7f29\u7b97\u6cd5\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u6295\u5c4f\u7684\u6d41\u7545\u6027\u548c\u7a33\u5b9a\u6027\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u5904\u7406\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u4f7f\u7528Python\u6295\u5c4f\uff1f<\/strong><br \/>Python\u7684\u6295\u5c4f\u529f\u80fd\u53ef\u4ee5\u5728\u591a\u4e2a\u5e73\u53f0\u4e0a\u5b9e\u73b0\uff0c\u5305\u62ecWindows\u3001macOS\u548cLinux\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u5e93\u6216\u5de5\u5177\u6765\u652f\u6301\u6295\u5c4f\u529f\u80fd\u3002\u5728\u9009\u62e9\u5e93\u65f6\uff0c\u786e\u4fdd\u5176\u517c\u5bb9\u6027\uff0c\u5e76\u67e5\u770b\u76f8\u5173\u6587\u6863\u4ee5\u83b7\u53d6\u5177\u4f53\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u8bf4\u660e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8fdb\u884c\u6295\u5c4f\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u5982PyChromecast\u5b9e\u73b0Chromecast\u6295\u5c4f\u3001\u4f7f\u7528O [&hellip;]","protected":false},"author":3,"featured_media":967217,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967210"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=967210"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967210\/revisions"}],"predecessor-version":[{"id":967219,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967210\/revisions\/967219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/967217"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=967210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=967210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=967210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}