{"id":924083,"date":"2024-12-26T15:03:29","date_gmt":"2024-12-26T07:03:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/924083.html"},"modified":"2024-12-26T15:03:31","modified_gmt":"2024-12-26T07:03:31","slug":"%e5%a6%82%e4%bd%95python%e8%a7%86%e9%a2%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/924083.html","title":{"rendered":"\u5982\u4f55python\u89c6\u9891"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211905\/b9c59fe9-6227-4a51-adc7-aff01b2bb489.webp\" alt=\"\u5982\u4f55python\u89c6\u9891\" \/><\/p>\n<p><p> <strong>\u8981\u4f7f\u7528Python\u5904\u7406\u89c6\u9891\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u3001MoviePy\u5e93\u3001imageio\u5e93\u7b49<\/strong>\u3002\u8fd9\u4e09\u4e2a\u5e93\u5404\u6709\u5176\u4f18\u70b9\uff1aOpenCV\u4ee5\u5176\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u8457\u79f0\uff0c\u5e7f\u6cdb\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u548c\u56fe\u50cf\u5904\u7406\u9879\u76ee\uff1bMoviePy\u662f\u4e00\u6b3e\u7b80\u5355\u6613\u7528\u7684\u89c6\u9891\u7f16\u8f91\u5e93\uff0c\u9002\u5408\u5236\u4f5c\u548c\u7f16\u8f91\u89c6\u9891\uff1bimageio\u5219\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165\u56fe\u50cf\u548c\u89c6\u9891\u7684\u7075\u6d3b\u5e93\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b89\u88c5\u3001\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u8fdb\u884cPython\u89c6\u9891\u5904\u7406\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u4e0e\u51c6\u5907<\/strong><\/h2>\n<p><p>\u5728\u5f00\u59cb\u5904\u7406\u89c6\u9891\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fddPython\u73af\u5883\u4e2d\u5b89\u88c5\u4e86\u6240\u9700\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h2>OpenCV\u5b89\u88c5<\/h2>\n<\/p>\n<p><p>OpenCV\u662f\u6700\u5e38\u7528\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6\u3001\u5904\u7406\u548c\u5199\u5165\u89c6\u9891\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p>pip install opencv-python-headless  # \u5982\u679c\u4e0d\u9700\u8981GUI\u529f\u80fd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>MoviePy\u5b89\u88c5<\/h2>\n<\/p>\n<p><p>MoviePy\u662f\u4e00\u4e2a\u7528\u4e8e\u89c6\u9891\u7f16\u8f91\u7684Python\u5e93\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u89c6\u9891\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install moviepy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>Imageio\u5b89\u88c5<\/h2>\n<\/p>\n<p><p>Imageio\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165\u56fe\u50cf\u548c\u89c6\u9891\u7684\u5e93\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install imageio<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528OpenCV\u8fdb\u884c\u89c6\u9891\u5904\u7406<\/strong><\/h2>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5904\u7406\u89c6\u9891\u6587\u4ef6\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u8bfb\u53d6\u3001\u5904\u7406\u548c\u4fdd\u5b58\u89c6\u9891\u3002<\/p>\n<\/p>\n<p><h2>\u8bfb\u53d6\u548c\u663e\u793a\u89c6\u9891<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u663e\u793a\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u6253\u5f00\u89c6\u9891\u6587\u4ef6<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(&#39;video.mp4&#39;)<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    cv2.imshow(&#39;Video&#39;, frame)<\/p>\n<p>    if cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u89c6\u9891\u5e27\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u89c6\u9891\u7684\u540c\u65f6\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u5e27\u8fdb\u884c\u5904\u7406\uff0c\u4f8b\u5982\u8f6c\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    cv2.imshow(&#39;Gray Video&#39;, gray_frame)<\/p>\n<p>    if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u89c6\u9891<\/h2>\n<\/p>\n<p><p>\u5c06\u5904\u7406\u540e\u7684\u5e27\u4fdd\u5b58\u4e3a\u65b0\u7684\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">out = cv2.VideoWriter(&#39;output.avi&#39;, cv2.VideoWriter_fourcc(*&#39;XVID&#39;), 20.0, (640, 480))<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    processed_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    out.write(processed_frame)<\/p>\n<p>cap.release()<\/p>\n<p>out.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528MoviePy\u8fdb\u884c\u89c6\u9891\u7f16\u8f91<\/strong><\/h2>\n<p><p>MoviePy\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u9ad8\u5c42\u6b21\u7684\u63a5\u53e3\u6765\u5904\u7406\u89c6\u9891\uff0c\u9002\u5408\u7528\u4e8e\u751f\u6210\u548c\u7f16\u8f91\u89c6\u9891\u3002<\/p>\n<\/p>\n<p><h2>\u8bfb\u53d6\u548c\u526a\u8f91\u89c6\u9891<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528MoviePy\u8bfb\u53d6\u89c6\u9891\u5e76\u8fdb\u884c\u526a\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from moviepy.editor import VideoFileClip<\/p>\n<p>clip = VideoFileClip(&quot;video.mp4&quot;).subclip(10, 20)<\/p>\n<p>clip.preview()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u89c6\u9891\u8f6c\u6362\u548c\u7f16\u8f91<\/h2>\n<\/p>\n<p><p>MoviePy\u5141\u8bb8\u8f6c\u6362\u89c6\u9891\u683c\u5f0f\u3001\u6dfb\u52a0\u7279\u6548\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u89c6\u9891\u8f6c\u4e3aGIF<\/p>\n<p>clip.write_gif(&quot;output.gif&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6c34\u5370<\/strong><\/h2>\n<p>watermark = VideoFileClip(&quot;watermark.png&quot;)<\/p>\n<p>final_clip = clip.set_duration(10).fx(vfx.composite, watermark, x=10, y=10)<\/p>\n<p>final_clip.write_videofile(&quot;output.mp4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u89c6\u9891\u5408\u6210<\/h2>\n<\/p>\n<p><p>\u5408\u6210\u591a\u4e2a\u89c6\u9891\u526a\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from moviepy.editor import concatenate_videoclips<\/p>\n<p>clip1 = VideoFileClip(&quot;part1.mp4&quot;)<\/p>\n<p>clip2 = VideoFileClip(&quot;part2.mp4&quot;)<\/p>\n<p>final_clip = concatenate_videoclips([clip1, clip2])<\/p>\n<p>final_clip.write_videofile(&quot;combined.mp4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528Imageio\u8fdb\u884c\u89c6\u9891\u5904\u7406<\/strong><\/h2>\n<p><p>Imageio\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165\u89c6\u9891\u6587\u4ef6\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u8bfb\u53d6\u89c6\u9891\u5e27<\/h2>\n<\/p>\n<p><p>Imageio\u53ef\u4ee5\u9010\u5e27\u8bfb\u53d6\u89c6\u9891\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import imageio<\/p>\n<p>reader = imageio.get_reader(&#39;video.mp4&#39;)<\/p>\n<p>for frame in reader:<\/p>\n<p>    # \u5904\u7406\u6bcf\u4e00\u5e27<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4fdd\u5b58\u89c6\u9891\u5e27<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u5904\u7406\u540e\u7684\u5e27\u4fdd\u5b58\u4e3a\u65b0\u7684\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">writer = imageio.get_writer(&#39;output.mp4&#39;, fps=30)<\/p>\n<p>for frame in reader:<\/p>\n<p>    processed_frame = process(frame)  # \u7528\u6237\u5b9a\u4e49\u7684\u5904\u7406\u51fd\u6570<\/p>\n<p>    writer.append_data(processed_frame)<\/p>\n<p>writer.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u89c6\u9891\u5904\u7406\u5e94\u7528\u793a\u4f8b<\/strong><\/h2>\n<p><p>\u7ed3\u5408\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u5e93\uff0c\u4e0b\u9762\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u89c6\u9891\u5904\u7406\u793a\u4f8b\uff1a\u5c06\u89c6\u9891\u8f6c\u4e3a\u7070\u5ea6\u5e76\u6dfb\u52a0\u6c34\u5370\u3002<\/p>\n<\/p>\n<p><h2>\u5b8c\u6574\u793a\u4f8b<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>from moviepy.editor import VideoFileClip, vfx<\/p>\n<h2><strong>\u4f7f\u7528OpenCV\u8bfb\u53d6\u89c6\u9891\u5e76\u8f6c\u4e3a\u7070\u5ea6<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(&#39;video.mp4&#39;)<\/p>\n<p>fourcc = cv2.VideoWriter_fourcc(*&#39;XVID&#39;)<\/p>\n<p>out = cv2.VideoWriter(&#39;temp.avi&#39;, fourcc, 20.0, (640, 480))<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    out.write(gray_frame)<\/p>\n<p>cap.release()<\/p>\n<p>out.release()<\/p>\n<h2><strong>\u4f7f\u7528MoviePy\u6dfb\u52a0\u6c34\u5370<\/strong><\/h2>\n<p>clip = VideoFileClip(&#39;temp.avi&#39;)<\/p>\n<p>watermark = VideoFileClip(&quot;watermark.png&quot;)<\/p>\n<p>final_clip = clip.fx(vfx.composite, watermark, x=10, y=10)<\/p>\n<p>final_clip.write_videofile(&quot;final_output.mp4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528Python\u7684\u591a\u4e2a\u5e93\u6765\u8fdb\u884c\u89c6\u9891\u5904\u7406\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u5e76\u5b9e\u73b0\u590d\u6742\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u89c6\u9891\u5904\u7406\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\u6765\u5904\u7406\u89c6\u9891\uff0c\u5982OpenCV\u3001MoviePy\u548cFFmpeg\u3002\u4f7f\u7528OpenCV\u53ef\u4ee5\u8fdb\u884c\u89c6\u9891\u7684\u8bfb\u53d6\u3001\u5199\u5165\u548c\u5e27\u5904\u7406\uff0c\u800cMoviePy\u5219\u66f4\u9002\u5408\u8fdb\u884c\u89c6\u9891\u526a\u8f91\u3001\u5408\u6210\u548c\u7279\u6548\u6dfb\u52a0\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5b9e\u73b0\u89c6\u9891\u5904\u7406\u3002<\/p>\n<p><strong>Python\u89c6\u9891\u5904\u7406\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u5728\u5f00\u59cb\u5904\u7406\u89c6\u9891\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecOpenCV\uff08\u4f7f\u7528<code>pip install opencv-python<\/code>\u5b89\u88c5\uff09\u3001MoviePy\uff08\u4f7f\u7528<code>pip install moviepy<\/code>\u5b89\u88c5\uff09\u548cNumPy\uff08\u5904\u7406\u6570\u7ec4\u6570\u636e\uff09\u3002\u786e\u4fdd\u5728\u73af\u5883\u4e2d\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u4ee5\u4fbf\u987a\u5229\u8fdb\u884c\u89c6\u9891\u5904\u7406\u3002<\/p>\n<p><strong>Python\u5904\u7406\u89c6\u9891\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u89c6\u9891\u5904\u7406\u7684\u5e94\u7528\u573a\u666f\u975e\u5e38\u5e7f\u6cdb\uff0c\u5305\u62ec\u89c6\u9891\u526a\u8f91\u3001\u7279\u6548\u6dfb\u52a0\u3001\u89c6\u9891\u5206\u6790\uff08\u5982\u7269\u4f53\u68c0\u6d4b\u548c\u8ddf\u8e2a\uff09\u3001\u89c6\u9891\u8f6c\u7801\u4ee5\u53ca\u5408\u6210\u591a\u5a92\u4f53\u5185\u5bb9\u7b49\u3002\u65e0\u8bba\u662f\u7528\u4e8e\u5a31\u4e50\u8fd8\u662f\u7814\u7a76\uff0cPython\u90fd\u80fd\u6ee1\u8db3\u4e0d\u540c\u9700\u6c42\uff0c\u63d0\u4f9b\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7f\u7528Python\u5904\u7406\u89c6\u9891\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u3001MoviePy\u5e93\u3001imageio\u5e93\u7b49\u3002\u8fd9\u4e09\u4e2a\u5e93\u5404\u6709\u5176\u4f18\u70b9\uff1a [&hellip;]","protected":false},"author":3,"featured_media":924087,"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\/924083"}],"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=924083"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/924083\/revisions"}],"predecessor-version":[{"id":924090,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/924083\/revisions\/924090"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/924087"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=924083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=924083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=924083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}