{"id":598,"date":"2024-01-29T19:55:33","date_gmt":"2024-01-29T11:55:33","guid":{"rendered":"https:\/\/himpqblog.cn\/?p=598"},"modified":"2024-02-06T18:19:58","modified_gmt":"2024-02-06T10:19:58","slug":"pygame-%e6%92%ad%e6%94%be%e8%a7%86%e9%a2%91%e4%b8%8e%e9%9f%b3%e8%a7%86%e9%a2%91%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/himpqblog.cn\/index.php\/archives\/598","title":{"rendered":"Pygame \u64ad\u653e\u89c6\u9891 \/ \u97f3\u89c6\u9891\u540c\u6b65 \/ \u64ad\u653e\u591a\u6bb5\u77ed\u8fde\u7eed\u97f3\u9891\u4ea7\u751f\u7206\u97f3"},"content":{"rendered":"\n<p>\u6700\u8fd1\u7a81\u7136\u60f3\u8d77\u624b\u4e0a\u4e71\u5199\u7684\u4e00\u4e2a\u9879\u76ee\uff0c\u62ffpygame\u5b9e\u73b0\u5404\u79cd\u529f\u80fd\u7684\u7ec4\u4ef6\uff0c\u5176\u4e2d\u5c31\u6709\u4e00\u4e2a\u89c6\u9891\u7ec4\u4ef6\u6ca1\u505a\u597d\uff0c\u5361\u5728\u5fc3\u5934\uff0c\u4fbf\u60f3\u529e\u6cd5\u91cd\u65b0\u5f04\u4e0b<\/p>\n\n\n\n<p>\u8be5\u7ec4\u4ef6\u6ca1\u4f7f\u7528opencv\u592a\u9ebb\u70e6\u4e86\uff0c\u7528\u4e86moviepy\u65b9\u4fbf<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u64ad\u653e\u89c6\u9891<\/h2>\n\n\n\n<p>\u81f3\u4e8e\u64ad\u653e\u89c6\u9891\u5c31\u5f88\u7b80\u5355\u4e86\uff0c\u76f4\u63a5\u628a\u7a97\u53e3\u5e27\u7387\u6539\u6210\u89c6\u9891\u7684\u5e27\u7387\uff0c\u6bcf\u79d2\u8fdb\u884c\u7ed8\u753b\u5373\u53ef\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef draw(self, x=None, y=None):\n        if not self.isPlaying:\n            if self.surface:\n                self._BlitOnScreen(self.surface, (x, y))\n                return\n            if self.coverSurface == None:\n                self.coverPic = self.videoClip.get_frame(0)\n                self.coverSurface = pygame.surfarray.make_surface(self.coverPic.swapaxes(0, 1))\n                self.coverSurface = pygame.transform.scale(self.coverSurface, (self.width, self.height))\n\n            self._BlitOnScreen(self.coverSurface, (x, y))\n            return\n        \n        threading.Thread(target=self._playAudioPerFrame).start()  # \u64ad\u653e\u97f3\u9891\n        \n        frame      = self.videoClip.get_frame(self.frames&#x5B;self.nowIndex])\n        self.nowIndex += 1\n        surface = pygame.surfarray.make_surface(frame.swapaxes(0, 1))\n        surface = pygame.transform.scale(surface, (self.width, self.height))\n        self._BlitOnScreen(surface, (x, y))\n        \n        self.audioFinished.wait()  # \u7b49\u5f85\u8be5\u5e27\u7684\u97f3\u9891\u64ad\u653e\u5b8c\u6bd5\n<\/pre><\/div>\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u53ea\u662f\u8be5\u7ec4\u4ef6\u7684\u4e00\u90e8\u5206\uff0c\u6bcf\u6b21\u7a97\u53e3\u5237\u65b0\u65f6\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5224\u65ad\u8be5\u7ec4\u4ef6\u662f\u5426\u5904\u4e8e\u64ad\u653e\u72b6\u6001\uff0c\u82e5\u662f\u64ad\u653e\u72b6\u6001\u5219\u83b7\u53d6\u5f53\u524d\u89c6\u9891\u5e27<\/p>\n\n\n\n<p>\uff08<strong><em>self.videoClip.get_frame(self.frames[self.nowIndex])<\/em><\/strong> \u7528\u4e8e\u83b7\u53d6\u89c6\u9891\u5e27<\/p>\n\n\n\n<p>\u5176\u4e2d <strong><em>self.videoClip = VideoFileClip(path)<\/em><\/strong><\/p>\n\n\n\n<p>\u4e14     <strong><em>from moviepy.editor import VideoFileClip<\/em><\/strong><\/p>\n\n\n\n<p><strong><em>self.nowIndex<\/em><\/strong>\u7528\u4e8e\u8bb0\u5f55\u5f53\u524d\u64ad\u653e\u5230\u7684\u89c6\u9891\u5e27\u6570\uff09<\/p>\n\n\n\n<p>\u5e76\u4e14\u5728\u7ed8\u5236\u56fe\u50cf\u524d\u5148\u542f\u52a8\u4e86\u7ebf\u7a0b\u8fdb\u884c\u97f3\u9891\u7684\u64ad\u653e\uff0c\u5e76\u5728\u7ed8\u5236\u5b8c\u6210\u7b49\u5f85\u97f3\u9891\u64ad\u653e\u5b8c\u6210\u3002\uff08<strong><em>self.audioFinished<\/em><\/strong>\u53ea\u662f\u4e00\u4e2a<strong><em>threading.Event<\/em><\/strong>\u7528\u4e8e\u7ebf\u7a0b\u95f4\u4e92\u76f8\u7b49\u5f85\uff0c\u800c\u7ed8\u5236\u8fc7\u7a0b\u4e8e\u4e3b\u7ebf\u7a0b\u5b8c\u6210\uff0c\u97f3\u9891\u64ad\u653e\u53e6\u8d77\u65b0\u7ebf\u7a0b\uff09<\/p>\n\n\n\n<p>\u81f3\u4e8e\u63a7\u5236\u7a97\u53e3\u5237\u65b0\u5e27\u7387\uff0c\u5728\u4e3b\u5faa\u73af\u4e2d <em><strong>time.sleep(1\/video_fps)<\/strong><\/em> \u5373\u53ef<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u97f3\u89c6\u9891\u540c\u6b65 &#8211; 1<\/h2>\n\n\n\n<p>\u4e0a\u9762\u63d0\u5230\u4e86\u4f7f\u7528<strong><em>threading.Event<\/em><\/strong>\u63a7\u5236\u4e86\u97f3\u9891\u64ad\u653e\u7ebf\u7a0b\u548c\u89c6\u9891\u64ad\u653e\u7684\u540c\u6b65\uff0c\u7136\u800c\u8fd9\u5e76\u4e0d\u8db3\u591f\u3002<\/p>\n\n\n\n<p>\u4e0d\u5f97\u4e0d\u8bf4<strong><em>moviepy<\/em><\/strong>\u7684\u529f\u80fd\u771f\u5b8c\u5584\uff0c\u5728\u4f60\u4f7f\u7528 <strong><em>VideoFileClip<\/em><\/strong> \u5bfc\u5165\u89c6\u9891\u540e\uff0c\u8be5\u5bf9\u8c61\u5c31\u6709\u4e86\u4e00\u4e2a <strong><em>audio<\/em><\/strong> \u5c5e\u6027\uff0c\u5373\u4e00\u4e2a <strong><em>AudioClip<\/em><\/strong> \u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u65f6\u523b\u6765\u83b7\u53d6\u97f3\u9891\u5e27\u3002<\/p>\n\n\n\n<p>\u5728\u97f3\u89c6\u9891\u540c\u6b65\u524d\uff0c\u4e0d\u5f97\u4e0d\u8bb2\u4e00\u4e0b\u9ad8\u4e2d\u4fe1\u606f\u6280\u672f\u7684\u5185\u5bb9\u4e86\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u97f3\u9891\u91c7\u6837\u7387\u548c\u57fa\u672c\u4fe1\u606f<\/h2>\n\n\n\n<p>\u97f3\u9891\u6587\u4ef6\u6709\u4e00\u4e2a\u72ec\u7279\u7684\u5c5e\u6027\uff0c\u97f3\u9891\u91c7\u6837\u7387\uff0c\u8fd9\u4e2a\u4e1c\u897f\u548c\u89c6\u9891\u7684\u5e27\u7387\u5dee\u4e0d\u591a\u662f\u4e00\u4e2a\u6982\u5ff5\uff0c\u5373\u6bcf\u79d2\u6709\u591a\u5c11\u4e2a\u97f3\u9891\u6570\u636e\u3002\uff08\u5927\u591a\u4e3a\u4e00\u4e2a <strong><em>[int, int]<\/em><\/strong> \u7c7b\u578b\u7684\u6570\u636e\u4e3a\u4e00\u4e2a\u91c7\u6837\uff09<\/p>\n\n\n\n<p>\u800c <strong><em>pygame<\/em><\/strong> \u652f\u6301\u901a\u8fc7 <strong><em>numpy<\/em><\/strong> \u505a\u4e2d\u95f4\u5546\u6765\u5c06\u4e00\u4e2a <strong><em>[[int, int], [int, int], &#8230;]<\/em><\/strong> \u7c7b\u578b\u7684\u6570\u7ec4\uff08\u5373\u67d0\u4e2a\u65f6\u95f4\u6bb5\u7684\u91c7\u6837\uff09\u8f6c\u53d8\u4e3apygame\u7684 <strong><em>Sound<\/em><\/strong> \u5bf9\u8c61\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npygame.sndarray.make_sound(array)\n<\/pre><\/div>\n\n\n<p>\u800c <strong><em>moviepy<\/em><\/strong> \u7684 <strong><em>AudioClip<\/em><\/strong> \u53ef\u4ee5\u901a\u8fc7 <em><strong>to_soundarray<\/strong><\/em> \u65b9\u6cd5\u6765\u83b7\u53d6\u67d0\u4e2a\u65f6\u95f4\u6216\u65f6\u95f4\u6570\u7ec4\u65f6\uff08\u5373 <strong><em>[time1, time2, time3, &#8230;]<\/em><\/strong> \u7c7b\u578b\u7684\u6570\u7ec4\uff09\u7684\u91c7\u6837\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nAudioClip.to_soundarray(self, tt=None, fps=None, quantize=False, nbytes=2, buffersize=50000)\n<\/pre><\/div>\n\n\n<p>\u5176\u4e2d<strong><em>tt<\/em><\/strong>\u5c31\u662f\u4e0a\u8ff0\u7684\u65f6\u95f4\u6216\u65f6\u95f4\u7684\u6570\u7ec4\u3002<\/p>\n\n\n\n<p>\u76f8\u8f85\u76f8\u6210\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u97f3\u89c6\u9891\u540c\u6b65 &#8211; 2<\/h2>\n\n\n\n<p>\u65e2\u77e5\u5982\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u7b97\u51fa\u6bcf\u5e27\u64ad\u653e\u591a\u5c11\u4e2a\u91c7\u6837\u5373\u53ef\u3002<\/p>\n\n\n\n<p><span class=\"MathJax_Preview\" style=\"color: inherit; display: none;\"><\/span><span class=\"MathJax\" id=\"MathJax-Element-11-Frame\" tabindex=\"0\" data-mathml=\"<math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot;><mfrac><mfrac><mn>1<\/mn><mtext>\u89c6\u9891\u5e27\u7387<\/mtext><\/mfrac><mfrac><mn>1<\/mn><mtext>\u97f3\u9891\u91c7\u6837\u7387<\/mtext><\/mfrac><\/mfrac><mo>=<\/mo><mtext>\u6bcf\u89c6\u9891\u5e27\u64ad\u653e\u97f3\u9891\u91c7\u6837\u6570<\/mtext><\/math>&#8221; role=&#8221;presentation&#8221; style=&#8221;position: relative;&#8221;><nobr aria-hidden=\"true\"><span class=\"math\" id=\"MathJax-Span-89\" style=\"width: 15.951em; display: inline-block;\"><span style=\"display: inline-block; position: relative; width: 13.748em; height: 0px; font-size: 116%;\"><span style=\"position: absolute; clip: rect(0.673em, 1013.75em, 3.546em, -999.998em); top: -2.344em; left: 0em;\"><span class=\"mrow\" id=\"MathJax-Span-90\"><span class=\"mfrac\" id=\"MathJax-Span-91\"><span style=\"display: inline-block; position: relative; width: 2.636em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.211em, 1002.11em, 4.648em, -999.998em); top: -4.883em; left: 50%; margin-left: -1.051em;\"><span class=\"mfrac\" id=\"MathJax-Span-92\"><span style=\"display: inline-block; position: relative; width: 1.87em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.546em, 1000.19em, 4.169em, -999.998em); top: -4.356em; left: 50%; margin-left: -0.141em;\"><span class=\"mn\" id=\"MathJax-Span-93\" style=\"font-size: 50%; font-family: MathJax_Main;\">1<\/span><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(1.822em, 1001.73em, 2.589em, -999.998em); top: -1.961em; left: 50%; margin-left: -0.86em;\"><span class=\"mtext\" id=\"MathJax-Span-94\" style=\"font-size: 50%;\"><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u89c6<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u9891<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u5e27<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u7387<\/span><\/span><span style=\"display: inline-block; width: 0px; height: 2.349em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1001.87em, 1.2em, -999.998em); top: -1.195em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 1.87em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(3.211em, 1002.54em, 4.648em, -999.998em); top: -3.446em; left: 50%; margin-left: -1.243em;\"><span class=\"mfrac\" id=\"MathJax-Span-95\"><span style=\"display: inline-block; position: relative; width: 2.301em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.546em, 1000.19em, 4.169em, -999.998em); top: -4.356em; left: 50%; margin-left: -0.141em;\"><span class=\"mn\" id=\"MathJax-Span-96\" style=\"font-size: 50%; font-family: MathJax_Main;\">1<\/span><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(1.822em, 1002.16em, 2.589em, -999.998em); top: -1.961em; left: 50%; margin-left: -1.051em;\"><span class=\"mtext\" id=\"MathJax-Span-97\" style=\"font-size: 50%;\"><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u97f3<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u9891<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u91c7<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u6837<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u7387<\/span><\/span><span style=\"display: inline-block; width: 0px; height: 2.349em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1002.3em, 1.2em, -999.998em); top: -1.195em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 2.301em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1002.64em, 1.2em, -999.998em); top: -1.291em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 2.636em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><span class=\"mo\" id=\"MathJax-Span-98\" style=\"font-family: MathJax_Main; padding-left: 0.29em;\">=<\/span><span class=\"mtext\" id=\"MathJax-Span-99\" style=\"padding-left: 0.29em;\"><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u6bcf<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u89c6<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u9891<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u5e27<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u64ad<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u653e<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u97f3<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u9891<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u91c7<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u6837<\/span><span style=\"font-family: STIXGeneral, &quot;Arial Unicode MS&quot;, serif; font-size: 86%; font-style: normal; font-weight: normal;\">\u6570<\/span><\/span><\/span><\/span><\/span><\/span><\/nobr><span class=\"MJX_Assistive_MathML\" role=\"presentation\"><span class=\"MathJax\" id=\"MathJax-Element-12-Frame\" tabindex=\"0\" data-mathml=\"<math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot;><mfrac><mfrac><mn>1<\/mn><mtext>\u89c6\u9891\u5e27\u7387<\/mtext><\/mfrac><mfrac><mn>1<\/mn><mtext>\u97f3\u9891\u91c7\u6837\u7387<\/mtext><\/mfrac><\/mfrac><mo>=<\/mo><mtext>\u6bcf\u89c6\u9891\u5e27\u64ad\u653e\u97f3\u9891\u91c7\u6837\u6570<\/mtext><\/math>&#8221; role=&#8221;presentation&#8221; style=&#8221;position: relative;&#8221;><nobr aria-hidden=\"true\"><span class=\"math\" id=\"MathJax-Span-100\" style=\"width: 15.951em; display: inline-block;\"><span style=\"display: inline-block; position: relative; width: 13.748em; height: 0px; font-size: 116%;\"><span style=\"position: absolute; clip: rect(0.673em, 1013.75em, 3.546em, -999.998em); top: -2.344em; left: 0em;\"><span class=\"mrow\" id=\"MathJax-Span-101\"><span class=\"mfrac\" id=\"MathJax-Span-102\"><span style=\"display: inline-block; position: relative; width: 2.636em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.211em, 1002.11em, 4.648em, -999.998em); top: -4.883em; left: 50%; margin-left: -1.051em;\"><span class=\"mfrac\" id=\"MathJax-Span-103\"><span style=\"display: inline-block; position: relative; width: 1.87em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.546em, 1000.19em, 4.169em, -999.998em); top: -4.356em; left: 50%; margin-left: -0.141em;\"><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(1.822em, 1001.73em, 2.589em, -999.998em); top: -1.961em; left: 50%; margin-left: -0.86em;\"><span style=\"display: inline-block; width: 0px; height: 2.349em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1001.87em, 1.2em, -999.998em); top: -1.195em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 1.87em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(3.211em, 1002.54em, 4.648em, -999.998em); top: -3.446em; left: 50%; margin-left: -1.243em;\"><span class=\"mfrac\" id=\"MathJax-Span-106\"><span style=\"display: inline-block; position: relative; width: 2.301em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.546em, 1000.19em, 4.169em, -999.998em); top: -4.356em; left: 50%; margin-left: -0.141em;\"><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(1.822em, 1002.16em, 2.589em, -999.998em); top: -1.961em; left: 50%; margin-left: -1.051em;\"><span style=\"display: inline-block; width: 0px; height: 2.349em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1002.3em, 1.2em, -999.998em); top: -1.195em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 2.301em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1002.64em, 1.2em, -999.998em); top: -1.291em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 2.636em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/nobr><span class=\"MJX_Assistive_MathML\" role=\"presentation\"><span class=\"MathJax\" id=\"MathJax-Element-13-Frame\" tabindex=\"0\" data-mathml=\"<math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot;><mfrac><mfrac><mn>1<\/mn><mtext>\u89c6\u9891\u5e27\u7387<\/mtext><\/mfrac><mfrac><mn>1<\/mn><mtext>\u97f3\u9891\u91c7\u6837\u7387<\/mtext><\/mfrac><\/mfrac><mo>=<\/mo><mtext>\u6bcf\u89c6\u9891\u5e27\u64ad\u653e\u97f3\u9891\u91c7\u6837\u6570<\/mtext><\/math>&#8221; role=&#8221;presentation&#8221; style=&#8221;position: relative;&#8221;><nobr aria-hidden=\"true\"><span class=\"math\" id=\"MathJax-Span-111\" style=\"width: 15.951em; display: inline-block;\"><span style=\"display: inline-block; position: relative; width: 13.748em; height: 0px; font-size: 116%;\"><span style=\"position: absolute; clip: rect(0.673em, 1013.75em, 3.546em, -999.998em); top: -2.344em; left: 0em;\"><span class=\"mrow\" id=\"MathJax-Span-112\"><span class=\"mfrac\" id=\"MathJax-Span-113\"><span style=\"display: inline-block; position: relative; width: 2.636em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.211em, 1002.11em, 4.648em, -999.998em); top: -4.883em; left: 50%; margin-left: -1.051em;\"><span class=\"mfrac\" id=\"MathJax-Span-114\"><span style=\"display: inline-block; position: relative; width: 1.87em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.546em, 1000.19em, 4.169em, -999.998em); top: -4.356em; left: 50%; margin-left: -0.141em;\"><\/span><span style=\"position: absolute; clip: rect(1.822em, 1001.73em, 2.589em, -999.998em); top: -1.961em; left: 50%; margin-left: -0.86em;\"><span style=\"display: inline-block; width: 0px; height: 2.349em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1001.87em, 1.2em, -999.998em); top: -1.195em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 1.87em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(3.211em, 1002.54em, 4.648em, -999.998em); top: -3.446em; left: 50%; margin-left: -1.243em;\"><span class=\"mfrac\" id=\"MathJax-Span-117\"><span style=\"display: inline-block; position: relative; width: 2.301em; height: 0px; margin-right: 0.146em; margin-left: 0.146em;\"><span style=\"position: absolute; clip: rect(3.546em, 1000.19em, 4.169em, -999.998em); top: -4.356em; left: 50%; margin-left: -0.141em;\"><\/span><span style=\"position: absolute; clip: rect(1.822em, 1002.16em, 2.589em, -999.998em); top: -1.961em; left: 50%; margin-left: -1.051em;\"><span style=\"display: inline-block; width: 0px; height: 2.349em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1002.3em, 1.2em, -999.998em); top: -1.195em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 2.301em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><span style=\"display: inline-block; width: 0px; height: 4.025em;\"><\/span><\/span><span style=\"position: absolute; clip: rect(0.864em, 1002.64em, 1.2em, -999.998em); top: -1.291em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 2.636em; height: 0px;\"><\/span><span style=\"display: inline-block; width: 0px; height: 1.056em;\"><\/span><\/span><\/span><\/span><\/span><span style=\"display: inline-block; width: 0px; height: 2.349em;\"><\/span><\/span><\/span><\/span><\/nobr><\/span><\/span><\/span><\/span><\/span><script>\\frac{\\frac{1}{\\text{\u89c6\u9891\u5e27\u7387}}}{\\frac{1}{\\text{\u97f3\u9891\u91c7\u6837\u7387}}} = \\text{\u6bcf\u89c6\u9891\u5e27\u64ad\u653e\u97f3\u9891\u91c7\u6837\u6570}<\/script><\/p>\n\n\n\n<p>\u5373\uff1a<\/p>\n\n\n\n<p>$\\frac{\\text{\u97f3\u9891\u91c7\u6837\u7387}}{\\text{\u89c6\u9891\u5e27\u7387}} = \\text{\u6bcf\u89c6\u9891\u5e27\u64ad\u653e\u97f3\u9891\u91c7\u6837\u6570}$<\/p>\n\n\n\n<p>\u7136\u540e\u5728\u6bcf\u6b21\u5e27\u5237\u65b0\u540e\u5f00\u4e00\u4e2a\u7ebf\u7a0b\u4e0e\u7ed8\u5236\u8fc7\u7a0b\u5e76\u53d1\u8fdb\u884c\u64ad\u653e\u64cd\u4f5c\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u5b9e\u73b0\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef _playAudioPerFrame(self):\n        self.audioFinished.clear()\n\n        nowVideoFrame = self.audioPlayPerVideoFrame * self.nowIndex\n        tovideoFrame  = nowVideoFrame + self.audioPlayPerVideoFrame\n\n        pos = numpy.array(range(nowVideoFrame, tovideoFrame))\n        frames = self.audio.to_soundarray(1\/self.audio.fps*pos, nbytes=self._nbytes, quantize=True)\n\n        chunk = pygame.sndarray.make_sound(frames)\n        chunk.set_volume(self.getVolume())\n\n        n = 0\n        while self.audioChannel.get_queue():\n            time.sleep(0.001)\n            n+=0.001\n            if n &gt;= 0.01:\n                print(&quot;timeout&quot;)\n                self.audioChannel.stop()\n                break\n        \n        self.audioChannel.queue(chunk)\n\n        self.audioFinished.set()\n<\/pre><\/div>\n\n\n<p>\u5176\u4e2d <strong><em>nowVideoFrame <\/em><\/strong>\u662f\u5f53\u524d\u64ad\u653e\u8d77\u59cb\u70b9\u91c7\u6837\uff0c\u800c <strong><em>tovideoFrame<\/em><\/strong> \u662f\u64ad\u653e\u7ec8\u70b9\u7684\u91c7\u6837\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7 <strong><em>range <\/em><\/strong>\u7b97\u51fa\u6240\u6709\u91c7\u6837\u7684\u65f6\u523b\uff08\u5373 <strong><em>pos <\/em><\/strong>\u53d8\u91cf\uff09\u8f6c\u4e3a <strong><em>numpy <\/em><\/strong>\u6570\u7ec4\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\u7b2c0~1\/60\u79d2\uff08\u5373\u4e00\u5e27\uff09\u7684 <strong><em>pos<\/em><\/strong> \u53d8\u91cf\u6b64\u65f6\u7684\u503c\u4e3a\uff1a<strong><em> [0, 1, &#8230;, 734] <\/em><\/strong>\uff08\u5047\u8bbe\u91c7\u6837\u7387\u4e3a44100Hz\uff09<\/p>\n\n\n\n<p>\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u83b7\u53d6\u8fd9\u4e9bID\u7684\u91c7\u6837\u3002\u800c to_soundarray \u9700\u8981\u4e00\u4e2a\u79d2\u4e3a\u5355\u4f4d\u7684\u65f6\u95f4\u6570\u7ec4\u3002\u8fd9\u65f6\u53ea\u9700\u62ff <strong><em>(1\/\u97f3\u9891\u91c7\u6837\u7387) * pos <\/em><\/strong>\u5373\u53ef\u5f97\u5230\u6bcf\u4e2a\u91c7\u6837\u5bf9\u5e94\u7684\u65f6\u523b<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u901a\u8fc7 <strong><em>self.audio.fps<\/em><\/strong> \u5373 <strong><em>AudioClip.fps<\/em><\/strong> \u5c5e\u6027\u83b7\u53d6\u97f3\u9891\u91c7\u6837\u7387\uff0c\u5177\u4f53\u53ef\u53c2\u8003 moviepy\u6587\u6863\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u53ea\u6709 numpy \u7684\u6570\u7ec4\u652f\u6301\u5c06\u6570\u636e\u4e58\u8fdb\u53bb\uff0cPython \u7684\u539f\u751f\u5217\u8868\u53ea\u4f1a\u50cf\u5b57\u7b26\u4e32\u90a3\u6837\u5c06\u5217\u8868\u91cd\u590dn\u904d\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"295\" src=\"https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/01\/image-4.png\" alt=\"\" class=\"wp-image-601\" srcset=\"https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/01\/image-4.png 733w, https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/01\/image-4-300x121.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"249\" height=\"73\" src=\"https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/01\/image-5.png\" alt=\"\" class=\"wp-image-602\"\/><\/figure>\n\n\n\n<p>\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u4e0a\u8ff0\u4ee3\u7801\u53c8\u8bbe\u7f6e\u4e86\u4e00\u4e2a <strong><em>while self.audioChannel.get_queue()<\/em><\/strong> \u5faa\u73af\uff0c\u662f\u56e0\u4e3a\u4e3a\u4e86\u9632\u6b62\u524d\u4e00\u4e2a\u97f3\u9891\u91c7\u6837\u8fd8\u672a\u64ad\u653e\u5b8c\u5c31\u7ee7\u7eed\u64ad\u653e\u4e0b\u4e00\u4e2a\u97f3\u9891\uff0c\u4f46 <strong><em>pygame<\/em><\/strong> \u7684 <strong><em>mixer.Channel <\/em><\/strong>\u5bf9\u8c61\u6709\u65f6\u4f1a\u56e0\u4e3a\u5176\u4ed6\u97f3\u9891\u7684\u64ad\u653e\u5361\u4f4f\uff0c\u6240\u4ee5\u5728\u5faa\u73af\u5185\u53c8\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u8d85\u65f6\u68c0\u6d4b\uff0c\u9632\u6b62\u5176\u4ed6\u97f3\u9891\u5361\u4f4f<strong><em> Channel<\/em><\/strong>\u3002<\/p>\n\n\n\n<p>\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528 <strong><em>Channel.play<\/em><\/strong> \u6765\u64ad\u653e\u91c7\u6837\uff0c\u662f\u56e0\u4e3a\u76f4\u63a5\u4f7f\u7528 <em><strong>play <\/strong><\/em>\u64ad\u653e\u4f1a\u4e2d\u65ad\u672a\u64ad\u653e\u7684\u91c7\u6837\uff0c\u5bfc\u81f4\u58f0\u97f3\u4f1a\u53d8\u5361\u3001\u523a\u8033\u3002\u800c<strong><em> Channel.queue <\/em><\/strong>\u4f1a\u5c06\u672a\u64ad\u653e\u7684\u91c7\u6837\u8ddf\u5728\u6b63\u5728\u64ad\u653e\u7684\u91c7\u6837\u7684\u540e\u9762\uff0c\u7b49\u5f85\u524d\u4e00\u4e2a\u91c7\u6837\u64ad\u653e\u5b8c\u6210\u518d\u64ad\u653e\u4e0b\u4e00\u91c7\u6837\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u4ee3\u7801\u7ed3\u5c3e\u653e\u4e0b <strong><em>audioFinished<\/em><\/strong> \u7684\u6807\u8bb0\uff0c\u4f7f\u5f97\u89c6\u9891\u5e27\u505c\u6b62\u7b49\u5f85\uff0c\u7ee7\u7eed\u7ed8\u5236\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7206\u97f3\u95ee\u9898 &#8211; Another solution<\/h2>\n\n\n\n<p>\u4e0d\u77e5\u4e3a\u4f55\uff0c\u4f7f\u7528\u8be5\u65b9\u6cd5\u64ad\u653e\u97f3\u9891\u65f6\u603b\u4f1a\u5bfc\u81f4\u968f\u673a\u7684\u7206\u97f3\uff0c\u521d\u6b65\u5206\u6790\u65f6\u89c9\u5f97\u662f\u91c7\u6837\u7387\u7684\u95ee\u9898\uff0c\u4f46\u662f\u65e0\u8bba\u662f\u6539 pygame.mixer \u7684\u64ad\u653e\u9891\u7387\u8fd8\u662f to_soundarray \u7684\u91c7\u6837\u7387\uff0c44100\u300122050 \u7684\u91c7\u6837\u7387\u751a\u81f3\u66f4\u4f4e\u3002<\/p>\n\n\n\n<p>\u5728\u7f51\u4e0a\u641c\u4e86\u4e00\u4e0b\uff0c\u53d1\u73b0\u662f\u7531\u4e8e\u4f7f\u7528 queue \u5bfc\u81f4\u58f0\u9053\u97f3\u9891\u7684\u91c7\u6837\u6ce2\u5f62\u65ad\u5d16\u4e0b\u5760\u81f3 0 \u4ea7\u751f\u7684\u95ee\u9898\uff08\u65b0\u7684\u77e5\u8bc6\uff09\uff0c\u201d\u53ef\u6015\u7684\u97f3\u9891\u566a\u97f3\u60ac\u5d16\u201c<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/32435091\/prevent-popping-at-end-of-sound-in-pygame-mixer\">python &#8211; Prevent &#8220;popping&#8221; at end of sound in Pygame.mixer &#8211; Stack Overflow<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/pygame\/pygame\/issues\/694\">Queuing very short sounds creates stuttering \u00b7 Issue #694 \u00b7 pygame\/pygame \u00b7 GitHub<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.reddit.com\/r\/pygame\/comments\/17a3xus\/pygame_audio_poppingcrackling_on_raspberry_pi_3\/?rdt=59199\">Pygame Audio Popping\/Crackling on Raspberry Pi 3 : r\/pygame (reddit.com)<\/a><\/p>\n\n\n\n<p>moviepy \u7684 preview \u4ea7\u751f\u7206\u97f3\u66f4\u5c0f\u662f\u56e0\u4e3a\u5b83\u4e0d\u50cf\u6211\u4eec\u542f\u7528\u591a\u4e2a\u7ebf\u7a0b\u800c\u662f\u4e00\u4e2a\u7ebf\u7a0b\u4e00\u76f4\u64ad\u653e\u4e00\u4e2a\u97f3\u9891\uff0c\u6211\u4eec\u5c1d\u8bd5\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u4e0d\u8003\u8651\u97f3\u89c6\u9891\u540c\u6b65\u7684\u95ee\u9898\uff08\u56e0\u4e3a\u540c\u6b65\u4f1a\u5bfc\u81f4\u4e0a\u8ff0\u95ee\u9898\uff09\uff0c\u53d1\u73b0\u566a\u97f3\u6d88\u5931\u4e86\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u97f3\u9891\u64ad\u653e\u7684\u901f\u7387\u4e0d\u53d8\uff0c\u57fa\u672c\u662f\u51c6\u786e\u7684\uff0c\u6211\u4eec\u8003\u8651\u4ece\u89c6\u9891\u64ad\u653e\u7aef\u63a7\u5236\u5ef6\u8fdf\uff08\u524d\u9762\u7684\u5185\u5bb9\u5bf9\u4e8e\u64ad\u653e\u4e00\u4e2a\u8981\u6c42\u4e0d\u9ad8\u7684\u89c6\u9891\u5df2\u7ecf\u5f88\u597d\u4e86\uff09\uff0c\u8fd9\u5c31\u5f88\u7b80\u5355\u4e86\uff0c\u53ea\u9700\u5c06\u97f3\u9891\u64ad\u653e\u5230\u7684\u5e27\u540c\u6b65\u5230\u89c6\u9891\u5e94\u8be5\u64ad\u653e\u7684\u5e27\u5373\u53ef\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"254\" src=\"https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/02\/image-1-1024x254.png\" alt=\"\" class=\"wp-image-611\" srcset=\"https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/02\/image-1-1024x254.png 1024w, https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/02\/image-1-300x74.png 300w, https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/02\/image-1-768x191.png 768w, https:\/\/himpqblog.cn\/wp-content\/uploads\/2024\/02\/image-1.png 1427w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\uff08\u4e0a\u9762\u7b2c56\u884c\u7684 <strong><em>self.nowIndex += 1<\/em><\/strong> \u4e5f\u53ef\u4ee5\u5220\u53bb\u4e86\uff0c\u8fd9\u6837\u5b50\u4e0d\u7ba1\u7a97\u53e3\u5e27\u7387\u662f\u591a\u5c11\uff0c\u90fd\u80fd\u4ee5\u56fa\u5b9a\u901f\u7387\u64ad\u653e\u89c6\u9891\u3002\uff09<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6848\u662f\u6700\u597d\u7684\uff0c\u97f3\u9891\u7aef\u65e0\u9700\u8003\u8651\u540c\u6b65\u95ee\u9898\uff0c\u56e0\u4e3a\u626c\u58f0\u5668\u5df2\u7ecf\u5e2e\u4f60\u5904\u7406\u4e86\uff08\u901a\u8fc7\u8bbe\u7f6e\u97f3\u9891\u91c7\u6837\u7387\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u7a81\u7136\u60f3\u8d77\u624b\u4e0a\u4e71\u5199\u7684\u4e00\u4e2a\u9879\u76ee\uff0c\u62ffpygame\u5b9e\u73b0\u5404\u79cd\u529f\u80fd\u7684\u7ec4\u4ef6\uff0c\u5176\u4e2d\u5c31\u6709\u4e00\u4e2a\u89c6\u9891\u7ec4\u4ef6\u6ca1\u505a\u597d\uff0c\u5361\u5728\u5fc3\u5934\uff0c\u4fbf\u60f3\u529e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=598"}],"version-history":[{"count":9,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":614,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/598\/revisions\/614"}],"wp:attachment":[{"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}