{"id":1112936,"date":"2025-01-08T17:43:07","date_gmt":"2025-01-08T09:43:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112936.html"},"modified":"2025-01-08T17:43:09","modified_gmt":"2025-01-08T09:43:09","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e7%9b%b4%e6%92%ad%e4%bf%9d%e5%ad%98%e4%b8%ba%e8%a7%86%e9%a2%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112936.html","title":{"rendered":"Python\u5982\u4f55\u5c06\u76f4\u64ad\u4fdd\u5b58\u4e3a\u89c6\u9891"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074742\/9c4c41f3-fc33-4d76-9fa0-1b4976506c1f.webp\" alt=\"Python\u5982\u4f55\u5c06\u76f4\u64ad\u4fdd\u5b58\u4e3a\u89c6\u9891\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u7528\u4e8e\u5c06\u76f4\u64ad\u4fdd\u5b58\u4e3a\u89c6\u9891\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528OpenCV\u3001\u4f7f\u7528FFmpeg\u5e93\u3001\u4f7f\u7528Pillow\u548cMoviePy\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c<strong>FFmpeg<\/strong> \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u591a\u5a92\u4f53\u5904\u7406\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u89c6\u9891\u683c\u5f0f\u7684\u8f6c\u6362\u548c\u6d41\u5a92\u4f53\u5904\u7406\u3002<strong>\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528FFmpeg\u6765\u4fdd\u5b58\u76f4\u64ad\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528OpenCV\u5b9e\u73b0\u76f4\u64ad\u89c6\u9891\u4fdd\u5b58<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u7684\u529f\u80fd\u3002\u4f7f\u7528OpenCV\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u76f4\u64ad\u89c6\u9891\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5OpenCV<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86OpenCV\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5OpenCV\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6355\u83b7\u76f4\u64ad\u89c6\u9891\u5e76\u4fdd\u5b58<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u6355\u83b7\u76f4\u64ad\u89c6\u9891\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u6253\u5f00\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<h2><strong>\u68c0\u67e5\u6444\u50cf\u5934\u662f\u5426\u6210\u529f\u6253\u5f00<\/strong><\/h2>\n<p>if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00\u6444\u50cf\u5934&quot;)<\/p>\n<p>    exit()<\/p>\n<h2><strong>\u83b7\u53d6\u89c6\u9891\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6<\/strong><\/h2>\n<p>frame_width = int(cap.get(3))<\/p>\n<p>frame_height = int(cap.get(4))<\/p>\n<h2><strong>\u5b9a\u4e49\u89c6\u9891\u7f16\u7801\u5668\u548c\u8f93\u51fa\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>fourcc = cv2.VideoWriter_fourcc(*&#39;XVID&#39;)<\/p>\n<p>out = cv2.VideoWriter(&#39;output.avi&#39;, fourcc, 20.0, (frame_width, frame_height))<\/p>\n<p>while True:<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        print(&quot;\u65e0\u6cd5\u63a5\u6536\u5e27&quot;)<\/p>\n<p>        break<\/p>\n<p>    # \u5199\u5165\u89c6\u9891\u6587\u4ef6<\/p>\n<p>    out.write(frame)<\/p>\n<p>    # \u663e\u793a\u89c6\u9891<\/p>\n<p>    cv2.imshow(&#39;frame&#39;, frame)<\/p>\n<p>    if cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>out.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6253\u5f00\u6444\u50cf\u5934\uff0c\u7136\u540e\u5b9a\u4e49\u89c6\u9891\u7f16\u7801\u5668\u548c\u8f93\u51fa\u6587\u4ef6\u540d\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u6355\u83b7\u6bcf\u4e00\u5e27\u89c6\u9891\uff0c\u5e76\u5c06\u5176\u5199\u5165\u5230\u8f93\u51fa\u6587\u4ef6\u4e2d\u3002\u5f53\u7528\u6237\u6309\u4e0b &#39;q&#39; \u952e\u65f6\uff0c\u5faa\u73af\u7ed3\u675f\u5e76\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528FFmpeg\u5b9e\u73b0\u76f4\u64ad\u89c6\u9891\u4fdd\u5b58<\/p>\n<\/p>\n<p><p>FFmpeg\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u591a\u5a92\u4f53\u5904\u7406\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u683c\u5f0f\u7684\u97f3\u89c6\u9891\u6587\u4ef6\u3002\u4f7f\u7528FFmpeg\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u76f4\u64ad\u89c6\u9891\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5FFmpeg<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86FFmpeg\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5FFmpeg\uff0c\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u7f51\u7ad9\u4e0a\u7684\u5b89\u88c5\u8bf4\u660e\uff1a<a href=\"https:\/\/ffmpeg.org\/download.html\">https:\/\/ffmpeg.org\/download.html<\/a><\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528FFmpeg\u4fdd\u5b58\u76f4\u64ad\u89c6\u9891<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528FFmpeg\u5c06\u76f4\u64ad\u89c6\u9891\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u5b9a\u4e49FFmpeg\u547d\u4ee4<\/strong><\/h2>\n<p>ffmpeg_command = [<\/p>\n<p>    &#39;ffmpeg&#39;,<\/p>\n<p>    &#39;-f&#39;, &#39;dshow&#39;,  # \u8f93\u5165\u683c\u5f0f<\/p>\n<p>    &#39;-i&#39;, &#39;video=Integrated Camera&#39;,  # \u8f93\u5165\u8bbe\u5907\u540d\u79f0<\/p>\n<p>    &#39;-vcodec&#39;, &#39;libx264&#39;,  # \u89c6\u9891\u7f16\u7801\u5668<\/p>\n<p>    &#39;-preset&#39;, &#39;ultrafast&#39;,  # \u7f16\u7801\u901f\u5ea6<\/p>\n<p>    &#39;output.mp4&#39;  # \u8f93\u51fa\u6587\u4ef6\u540d<\/p>\n<p>]<\/p>\n<h2><strong>\u8fd0\u884cFFmpeg\u547d\u4ee4<\/strong><\/h2>\n<p>subprocess.run(ffmpeg_command)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528subprocess\u6a21\u5757\u8fd0\u884cFFmpeg\u547d\u4ee4\u3002\u8be5\u547d\u4ee4\u5c06\u4ece\u6444\u50cf\u5934\u6355\u83b7\u89c6\u9891\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3aMP4\u683c\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Pillow\u548cMoviePy\u5b9e\u73b0\u76f4\u64ad\u89c6\u9891\u4fdd\u5b58<\/p>\n<\/p>\n<p><p>Pillow\u662f\u4e00\u4e2a\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u800cMoviePy\u662f\u4e00\u4e2a\u7528\u4e8e\u89c6\u9891\u7f16\u8f91\u7684\u5e93\u3002\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5e93\uff0c\u4f60\u53ef\u4ee5\u6355\u83b7\u76f4\u64ad\u89c6\u9891\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pillow\u548cMoviePy<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pillow\u548cMoviePy\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\u4eec\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow moviepy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Pillow\u548cMoviePy\u4fdd\u5b58\u76f4\u64ad\u89c6\u9891<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528Pillow\u548cMoviePy\u5c06\u76f4\u64ad\u89c6\u9891\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from PIL import ImageGrab<\/p>\n<p>from moviepy.editor import ImageSequenceClip<\/p>\n<h2><strong>\u5b9a\u4e49\u6355\u83b7\u533a\u57df<\/strong><\/h2>\n<p>bbox = (0, 0, 800, 600)<\/p>\n<h2><strong>\u6355\u83b7\u89c6\u9891\u5e27<\/strong><\/h2>\n<p>frames = []<\/p>\n<p>for _ in range(100):  # \u6355\u83b7100\u5e27<\/p>\n<p>    img = ImageGrab.grab(bbox=bbox)<\/p>\n<p>    frame = np.array(img)<\/p>\n<p>    frames.append(frame)<\/p>\n<h2><strong>\u5c06\u5e27\u8f6c\u6362\u4e3a\u89c6\u9891<\/strong><\/h2>\n<p>clip = ImageSequenceClip(frames, fps=20)<\/p>\n<p>clip.write_videofile(&#39;output.mp4&#39;, codec=&#39;libx264&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ImageGrab\u6a21\u5757\u6355\u83b7\u89c6\u9891\u5e27\uff0c\u5e76\u5c06\u6bcf\u4e00\u5e27\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528ImageSequenceClip\u7c7b\u5c06\u5e27\u8f6c\u6362\u4e3a\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5c06Python\u7528\u4e8e\u5c06\u76f4\u64ad\u4fdd\u5b58\u4e3a\u89c6\u9891\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u3001FFmpeg\u3001Pillow\u548cMoviePy\u7b49\u5e93\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u4ee3\u7801\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u6539\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u4fdd\u5b58\u76f4\u64ad\u89c6\u9891\uff1f<\/strong><br \/>\u4f7f\u7528Python\u4fdd\u5b58\u76f4\u64ad\u89c6\u9891\u901a\u5e38\u6d89\u53ca\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982OpenCV\u3001FFmpeg\u6216PyAV\u3002\u8fd9\u4e9b\u5e93\u80fd\u591f\u6355\u6349\u76f4\u64ad\u6d41\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u811a\u672c\u6765\u6307\u5b9a\u76f4\u64ad\u6e90\u5730\u5740\uff0c\u5e76\u8bbe\u7f6e\u89c6\u9891\u7f16\u7801\u683c\u5f0f\u548c\u4fdd\u5b58\u8def\u5f84\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5b9e\u65f6\u8f6c\u7801\u76f4\u64ad\u6d41\u5230\u4e0d\u540c\u683c\u5f0f\u7684\u89c6\u9891\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528FFmpeg\u7b49\u5de5\u5177\u5b9e\u73b0\u5b9e\u65f6\u8f6c\u7801\u3002\u901a\u8fc7Python\u811a\u672c\uff0c\u53ef\u4ee5\u8c03\u7528FFmpeg\u547d\u4ee4\uff0c\u5c06\u76f4\u64ad\u6d41\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u89c6\u9891\u683c\u5f0f\uff0c\u5982MP4\u3001AVI\u7b49\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8f6c\u6362\u683c\u5f0f\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u4fdd\u5b58\u76f4\u64ad\u89c6\u9891\u540e\uff0c\u5982\u4f55\u8fdb\u884c\u540e\u671f\u7f16\u8f91\uff1f<\/strong><br \/>\u5728\u4fdd\u5b58\u76f4\u64ad\u89c6\u9891\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u89c6\u9891\u7f16\u8f91\u5de5\u5177\u6216\u5e93\u8fdb\u884c\u540e\u671f\u5904\u7406\u3002Python\u4e2d\u7684MoviePy\u548cOpenCV\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u7528\u6237\u88c1\u526a\u3001\u5408\u5e76\u3001\u6dfb\u52a0\u7279\u6548\u6216\u97f3\u9891\u7b49\u3002\u8fd9\u4e9b\u529f\u80fd\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u521b\u5efa\u66f4\u5177\u5438\u5f15\u529b\u7684\u89c6\u9891\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u7528\u4e8e\u5c06\u76f4\u64ad\u4fdd\u5b58\u4e3a\u89c6\u9891\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528OpenCV\u3001\u4f7f\u7528FFmpeg\u5e93\u3001\u4f7f\u7528Pil [&hellip;]","protected":false},"author":3,"featured_media":1112945,"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\/1112936"}],"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=1112936"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112936\/revisions"}],"predecessor-version":[{"id":1112946,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112936\/revisions\/1112946"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112945"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}