{"id":185,"date":"2021-02-02T22:15:27","date_gmt":"2021-02-02T14:15:27","guid":{"rendered":"http:\/\/no-rule.tw\/?p=185"},"modified":"2022-04-17T14:36:33","modified_gmt":"2022-04-17T06:36:33","slug":"python-%e7%94%a8mpv%e6%92%ad%e6%94%be%e5%99%a8%e5%a5%97%e4%bb%b6%e4%b8%b2%e6%b5%81youtube%e5%bd%b1%e7%89%87","status":"publish","type":"post","link":"https:\/\/no-rule.tw\/?p=185","title":{"rendered":"[Python] \u7528mpv\u64ad\u653e\u5668\u5957\u4ef6\u4e32\u6d41Youtube\u5f71\u7247"},"content":{"rendered":"<p>\u9664\u4e86\u5148\u524d\u4ecb\u7d39\u7684<a href=\"http:\/\/no-rule.tw\/?p=175\">python-pafy\u5957\u4ef6<\/a>\u53ef\u4ee5\u7528\u4f86\u4e32\u806fyoutube\u5f71\u7247\u4ee5\u5916\uff0c\u4eca\u5929\u518d\u4ecb\u7d39\u53e6\u4e00\u500b\u5957\u4ef6<a href=\"https:\/\/github.com\/jaseg\/python-mpv\"> python-mpv<\/a>\uff0c\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u4e32\u806f\u5594! <a href=\"https:\/\/mpv.io\/\">mpv<\/a>\u672c\u8eab\u5c31\u662f\u4e00\u6b3e\u5bb9\u7269\u6574\u5408\u7684\u8f15\u91cf\u5316\u64ad\u653e\u5668\uff0c\u9664\u4e86\u529f\u80fd\u7c21\u55ae\u9084\u652f\u63f4\u8a31\u591a\u5e38\u898b\u7684\u683c\u5f0f! \u800c\u4e14\u9084\u662f\u958b\u6e90\u7684\u5594\uff0c\u503c\u5f97\u6df1\u5165\u7814\u7a76!<\/p>\n<div style=\"width: 448px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" class=\"\" src=\"https:\/\/i0.wp.com\/mpv.io\/images\/mpv-screenshot-34cd36ae.jpg?resize=438%2C275&#038;ssl=1\" alt=\"MPV player\" width=\"438\" height=\"275\"  data-recalc-dims=\"1\"><p class=\"wp-caption-text\">MPV player UI (source:<a href=\"https:\/\/mpv.io\/\">https:\/\/mpv.io\/<\/a>)<\/p><\/div>\n<p><!--more--><br \/>\npython-mpv\u9019\u500b\u5957\u4ef6\u4e5f\u662f\u9700\u8981\u4f7f\u7528youtube-dl\u9019\u500b\u5957\u4ef6\u4f86\u9032\u884c\u4e32\u6d41\uff0c\u6240\u4ee5\u6211\u5011\u4e5f\u5fc5\u9700\u5b89\u88dd\u9019\u500b\u5957\u4ef6\u3002 \u4e14\u7576juputer notebook \u5728\u57f7\u884c\u7684\u6642\u5f8c\u9084\u9700\u8981 mpv\u7684DLL\u6a94! \u6240\u4ee5\u4e0b\u9762\u6703\u624b\u628a\u624b\u6559\u5b78\uff0c\u6559\u5927\u5bb6\u5982\u4f55\u628apython-mpv\u74b0\u5883\u5efa\u8d77\u4f86\u3002<\/p>\n<h1>I.\u5b89\u88dd<\/h1>\n<p>\u9996\u5148\uff0c\u5148\u5b89\u88ddpython\u6703\u7528\u5230\u7684\u5957\u4ef6\uff0c\u6253\u958b\u547d\u4ee4\u63d0\u793a\u5b57\u5143cmd\u3002(\u8a18\u5f97\u4f7f\u7528\u7ba1\u7406\u54e1\u6b0a\u9650\u57f7\u884c)<\/p>\n<blockquote><p>pip install youtube-dl<br \/>\npip install python-mpv<\/p><\/blockquote>\n<p>\u63a5\u8457\u9700\u8981\u53bb\u4e0b\u8f09mpv-1.dll\u9019\u500b\u6a94\u6848\uff0c\u524d\u5f80<a href=\"https:\/\/sourceforge.net\/projects\/mpv-player-windows\/files\/libmpv\/\">https:\/\/sourceforge.net\/projects\/mpv-player-windows\/files\/libmpv\/<\/a><br \/>\n1.\u9078\u64c7 mpv-dev-x86_64 \u7684\u7248\u672c<br \/>\n<img loading=\"lazy\" class=\"\" title=\"\" src=\"https:\/\/i0.wp.com\/nr.img.no-rule.tw\/imgs\/p185\/mpv-1.png?resize=681%2C363&#038;ssl=1\" alt=\"\" width=\"681\" height=\"363\"  data-recalc-dims=\"1\"><br \/>\n&nbsp;<br \/>\n2.\u628a mpv-1.dll \u9019\u500b\u6a94\u6848\u89e3\u58d3\u7e2e\u5230python\u7684Scripts\u8cc7\u6599\u593e\u5e95\u4e0b\uff0c\u5982\u679c\u4f60\u8ddf\u672c\u602a\u4e00\u6a23\u7528anaconda\u5b89\u88dd\u7684\u8a71\u8def\u5f91\u6703\u5728&nbsp; C:\\Users\\<span style=\"color: #ff0000;\">{user_name}<\/span>\\anaconda3\\Scripts\\\uff0c\u89e3\u58d3\u7e2e\u9032\u53bb\u5c31\u5b8c\u6210\u4e86!<br \/>\n<img loading=\"lazy\" class=\"\" title=\"\" src=\"https:\/\/i0.wp.com\/nr.img.no-rule.tw\/imgs\/p185\/mpv-1_2.png?resize=524%2C297&#038;ssl=1\" alt=\"\" width=\"524\" height=\"297\"  data-recalc-dims=\"1\"><br \/>\n&nbsp;<\/p>\n<h1>II.Write Code<\/h1>\n<p>\u57fa\u672c\u7684mpv\u4e32\u6d41\u6bd4pafy\u9084\u5bb9\u6613\uff0c\u9078\u597dURL\u3001\u8a2d\u5b9a\u6253\u958bytdl(youtube-dl)\uff0c\u5c31\u53ef\u4ee5\u4e32\u806f\u4e0a\u4e86\u3002\u5c07\u4e0b\u9762\u7684code\u8cbc\u5230 jupyter notebook\u4e0a\u4e26\u57f7\u884c\uff0c\u5c31\u6703\u8df3\u51fa\u4e00\u500b\u8996\u7a97\u958b\u59cb\u64ad\u653e\u4e32\u6d41\u5f71\u7247\u4e86\u3002<\/p>\n<blockquote>\n<pre class=\"font-size:13 line-height:20 toolbar:1 lang:python decode:true\">import mpv\n# enable youtube-dl\nplayer = mpv.MPV(ytdl=True)\n# input a URL from youtube and play it.\nplayer.play('https:\/\/youtu.be\/79RLkH3T8hg')\nplayer.wait_for_playback()<\/pre>\n<\/blockquote>\n<p>\u6c92\u6709\u932f\u8aa4\u7684\u8a71\u5c31\u53ef\u4ee5\u770b\u5230\u4e32\u6d41\u6210\u529f\u7684\u5f71\u7247\u56c9!! \u5927\u5bb6\u5be6\u969b\u6bd4\u8f03\u770b\u770b\uff0c\u6703\u767c\u73fe\u6bd4 pafy\u7684\u756b\u8cea\u597d\u4e0a\u8a31\u591a\u5594!<br \/>\n<img loading=\"lazy\" class=\"\" title=\"\" src=\"https:\/\/i0.wp.com\/nr.img.no-rule.tw\/imgs\/p185\/mpv-output.png?resize=705%2C414&#038;ssl=1\" alt=\"\" width=\"705\" height=\"414\"  data-recalc-dims=\"1\"><br \/>\n\u4f46\u662f\uff0c\u7576\u95dc\u6389\u8996\u7a97\u7684\u6642\u5019 jupyter \u6703\u5831\u51fa\u932f\u8aa4\uff0c\u56e0\u70ba\u57f7\u884c\u7dd2\u88ab\u6211\u5011\u7d42\u6b62\u6389\u4e86! \u8981\u89e3\u6c7a\u9019\u6a23\u7684\u932f\u8aa4\u6211\u5011\u53ef\u4ee5\u5229\u7528\u4e00\u4e9b\u7a0b\u5f0f\u754c\u9762\u7684\u8996\u7a97\u4f86\u8655\u7406\uff0c\u50cf\u662fPyQt \uff0c\u4ee5\u4e0b\u5c31\u7528PyQt5\u751f\u6210\u4e00\u500b\u8996\u7a97\u3002<br \/>\n\u5982\u679c\u9084\u6c92\u6709\u5b89\u88ddPyQt5 \u7684\u8a71\u8981\u5148\u7528 pip install&nbsp;pyqt5 \u7684\u6307\u4ee4\u5b89\u88dd(\u8a73\u7d30\u7684\u8aaa\u660e\u53ef\u4ee5<a href=\"http:\/\/no-rule.tw\/?p=70\">\u53c3\u8003\u9019\u7bc7<\/a>)<\/p>\n<pre class=\"font-size:13 line-height:20 toolbar:1 lang:python decode:true\">#!\/usr\/bin\/env python3\nimport mpv\nimport sys\nfrom PyQt5.QtWidgets import *\nfrom PyQt5.QtCore import *\nclass Test(QMainWindow):\n    def __init__(self, parent=None):\n        super().__init__(parent)\n        self.container = QWidget(self)\n        self.setCentralWidget(self.container)\n        self.container.setAttribute(Qt.WA_DontCreateNativeAncestors)\n        self.container.setAttribute(Qt.WA_NativeWindow)\n        player = mpv.MPV(wid=str(int(self.container.winId())),\n                log_handler=print,\n                loglevel='debug')\n        player.play('https:\/\/youtu.be\/P96cju35j54')\napp = QApplication(sys.argv)\n# This is necessary since PyQT stomps over the locale settings needed by libmpv.\n# This needs to happen after importing PyQT before creating the first mpv.MPV instance.\nimport locale\nlocale.setlocale(locale.LC_NUMERIC, 'C')\nwin = Test()\nwin.show()\nsys.exit(app.exec_())<\/pre>\n<p>\u63a5\u8457\u57f7\u884c\u7a0b\u5f0f\u5c31\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u7684\u6548\u679c\u4e86! \u800c\u4e14\u56e0\u70ba\u662f\u6709 layout\u51fa\u8996\u7a97\uff0c\u6240\u4ee5\u9084\u53ef\u4ee5\u8abf\u6574\u8996\u7a97\u7684\u5927\u5c0f\u5594!<br \/>\n<img loading=\"lazy\" title=\"\" src=\"https:\/\/i0.wp.com\/nr.img.no-rule.tw\/imgs\/p185\/mpv-output-2.png?resize=689%2C472&#038;ssl=1\" alt=\"\" width=\"689\" height=\"472\"  data-recalc-dims=\"1\"><br \/>\n\u6210\u529f\u4e32\u6d41\u4ee5\u5f8c\uff0c\u5927\u5bb6\u6703\u60f3\u8981\u600e\u9ebc\u5229\u7528\u8207\u8b8a\u5316\u5462?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9664\u4e86\u5148\u524d\u4ecb\u7d39\u7684python-pafy\u5957\u4ef6\u53ef\u4ee5\u7528\u4f86\u4e32\u806fyoutube\u5f71\u7247\u4ee5\u5916\uff0c\u4eca\u5929\u518d\u4ecb\u7d39\u53e6\u4e00\u500b\u5957\u4ef6 python&#8230; &raquo; <a class=\"read-more-link\" href=\"https:\/\/no-rule.tw\/?p=185\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[6],"tags":[36,45,50],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/no-rule.tw\/index.php?rest_route=\/wp\/v2\/posts\/185"}],"collection":[{"href":"https:\/\/no-rule.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/no-rule.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/no-rule.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/no-rule.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=185"}],"version-history":[{"count":2,"href":"https:\/\/no-rule.tw\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/no-rule.tw\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/no-rule.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/no-rule.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/no-rule.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}