{"id":1027295,"date":"2024-12-31T10:49:47","date_gmt":"2024-12-31T02:49:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1027295.html"},"modified":"2024-12-31T10:49:50","modified_gmt":"2024-12-31T02:49:50","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e4%b8%80%e4%b8%aa%e8%a7%86%e9%a2%91%e6%97%b6%e9%95%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1027295.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u4e00\u4e2a\u89c6\u9891\u65f6\u957f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/43ba6fdd-bfc8-482e-bcea-9e08feb821e0.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u4e00\u4e2a\u89c6\u9891\u65f6\u957f\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528MoviePy\u3001OpenCV\u3001FFmpeg\u7b49\u5e93\uff0cMoviePy\u3001FFmpeg-python\u3001OpenCV\u3002\u4e0b\u9762\u4e3b\u8981\u4ecb\u7ecd\u4f7f\u7528MoviePy\u5e93\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528MoviePy\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5MoviePy\u5e93<\/li>\n<li>\u5bfc\u5165MoviePy<\/li>\n<li>\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6<\/li>\n<li>\u83b7\u53d6\u89c6\u9891\u65f6\u957f<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5MoviePy\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86MoviePy\u5e93\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install moviepy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165MoviePy<\/h3>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165MoviePy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from moviepy.editor import VideoFileClip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>VideoFileClip<\/code>\u7c7b\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">video_clip = VideoFileClip(&quot;path\/to\/your\/video.mp4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u83b7\u53d6\u89c6\u9891\u65f6\u957f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<code>duration<\/code>\u5c5e\u6027\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff0c\u5355\u4f4d\u4e3a\u79d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">video_duration = video_clip.duration<\/p>\n<p>print(f&quot;\u89c6\u9891\u65f6\u957f\u4e3a: {video_duration} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><strong>MoviePy<\/strong>\u662f\u4e00\u4e2a\u7528\u4e8e\u89c6\u9891\u7f16\u8f91\u7684Python\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u89c6\u9891\u6587\u4ef6\uff0c\u5305\u62ec\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u3002\u4f7f\u7528MoviePy\u5e93\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u7684\u6b65\u9aa4\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6\u5e76\u8bbf\u95ee\u5176<code>duration<\/code>\u5c5e\u6027\u5373\u53ef\u3002MoviePy\u81ea\u52a8\u5904\u7406\u4e86\u89c6\u9891\u6587\u4ef6\u7684\u5e95\u5c42\u7ec6\u8282\uff0c\u4f7f\u5f97\u8fd9\u4e00\u4efb\u52a1\u53d8\u5f97\u975e\u5e38\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528MoviePy\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from moviepy.editor import VideoFileClip<\/p>\n<h2><strong>\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6<\/strong><\/h2>\n<p>video_path = &quot;path\/to\/your\/video.mp4&quot;<\/p>\n<p>video_clip = VideoFileClip(video_path)<\/p>\n<h2><strong>\u83b7\u53d6\u89c6\u9891\u65f6\u957f<\/strong><\/h2>\n<p>video_duration = video_clip.duration<\/p>\n<p>print(f&quot;\u89c6\u9891\u65f6\u957f\u4e3a: {video_duration} \u79d2&quot;)<\/p>\n<h2><strong>\u5c06\u79d2\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u683c\u5f0f<\/strong><\/h2>\n<p>hours = int(video_duration \/\/ 3600)<\/p>\n<p>minutes = int((video_duration % 3600) \/\/ 60)<\/p>\n<p>seconds = int(video_duration % 60)<\/p>\n<p>print(f&quot;\u89c6\u9891\u65f6\u957f\u4e3a: {hours} \u5c0f\u65f6 {minutes} \u5206\u949f {seconds} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5176\u4ed6\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86MoviePy\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff0c\u5982OpenCV\u548cFFmpeg-python\u3002\u4ee5\u4e0b\u662f\u8fd9\u4e9b\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528OpenCV<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u89c6\u9891\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528OpenCV\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6<\/strong><\/h2>\n<p>video_path = &quot;path\/to\/your\/video.mp4&quot;<\/p>\n<p>video_capture = cv2.VideoCapture(video_path)<\/p>\n<h2><strong>\u83b7\u53d6\u89c6\u9891\u5e27\u6570\u548c\u5e27\u7387<\/strong><\/h2>\n<p>frame_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))<\/p>\n<p>frame_rate = video_capture.get(cv2.CAP_PROP_FPS)<\/p>\n<h2><strong>\u8ba1\u7b97\u89c6\u9891\u65f6\u957f<\/strong><\/h2>\n<p>video_duration = frame_count \/ frame_rate<\/p>\n<p>print(f&quot;\u89c6\u9891\u65f6\u957f\u4e3a: {video_duration} \u79d2&quot;)<\/p>\n<h2><strong>\u5c06\u79d2\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u683c\u5f0f<\/strong><\/h2>\n<p>hours = int(video_duration \/\/ 3600)<\/p>\n<p>minutes = int((video_duration % 3600) \/\/ 60)<\/p>\n<p>seconds = int(video_duration % 60)<\/p>\n<p>print(f&quot;\u89c6\u9891\u65f6\u957f\u4e3a: {hours} \u5c0f\u65f6 {minutes} \u5206\u949f {seconds} \u79d2&quot;)<\/p>\n<h2><strong>\u91ca\u653e\u89c6\u9891\u6355\u6349\u5bf9\u8c61<\/strong><\/h2>\n<p>video_capture.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528FFmpeg-python<\/h3>\n<\/p>\n<p><p>FFmpeg\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u591a\u5a92\u4f53\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5904\u7406\u97f3\u89c6\u9891\u6587\u4ef6\u3002FFmpeg-python\u662fFFmpeg\u7684Python\u7ed1\u5b9a\uff0c\u63d0\u4f9b\u4e86\u4e0eFFmpeg\u547d\u4ee4\u884c\u5de5\u5177\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528FFmpeg-python\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ffmpeg<\/p>\n<h2><strong>\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6<\/strong><\/h2>\n<p>video_path = &quot;path\/to\/your\/video.mp4&quot;<\/p>\n<p>probe = ffmpeg.probe(video_path)<\/p>\n<h2><strong>\u83b7\u53d6\u89c6\u9891\u65f6\u957f<\/strong><\/h2>\n<p>video_duration = float(probe[&#39;format&#39;][&#39;duration&#39;])<\/p>\n<p>print(f&quot;\u89c6\u9891\u65f6\u957f\u4e3a: {video_duration} \u79d2&quot;)<\/p>\n<h2><strong>\u5c06\u79d2\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u683c\u5f0f<\/strong><\/h2>\n<p>hours = int(video_duration \/\/ 3600)<\/p>\n<p>minutes = int((video_duration % 3600) \/\/ 60)<\/p>\n<p>seconds = int(video_duration % 60)<\/p>\n<p>print(f&quot;\u89c6\u9891\u65f6\u957f\u4e3a: {hours} \u5c0f\u65f6 {minutes} \u5206\u949f {seconds} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528MoviePy\u3001OpenCV\u548cFFmpeg-python\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u5e93\u3002MoviePy\u63d0\u4f9b\u4e86\u6700\u7b80\u6d01\u7684API\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u4f7f\u7528\uff1bOpenCV\u529f\u80fd\u5f3a\u5927\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u590d\u6742\u7684\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\uff1bFFmpeg-python\u5219\u63d0\u4f9b\u4e86\u4e0eFFmpeg\u547d\u4ee4\u884c\u5de5\u5177\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u975e\u5e38\u7075\u6d3b\u3002\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6613\u8bfb\u7684\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u6df1\u5165\u7406\u89e3\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u53ea\u662f\u89c6\u9891\u5904\u7406\u7684\u4e00\u90e8\u5206\u9700\u6c42\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u540e\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u5e93\u8fdb\u884c\u66f4\u591a\u7684\u89c6\u9891\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u89c6\u9891\u526a\u8f91\u3001\u683c\u5f0f\u8f6c\u6362\u3001\u6dfb\u52a0\u6c34\u5370\u3001\u63d0\u53d6\u97f3\u9891\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u6269\u5c55\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u89c6\u9891\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528MoviePy\u8fdb\u884c\u89c6\u9891\u526a\u8f91<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from moviepy.editor import VideoFileClip<\/p>\n<h2><strong>\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6<\/strong><\/h2>\n<p>video_path = &quot;path\/to\/your\/video.mp4&quot;<\/p>\n<p>video_clip = VideoFileClip(video_path)<\/p>\n<h2><strong>\u8fdb\u884c\u89c6\u9891\u526a\u8f91\uff0c\u83b7\u53d6\u524d10\u79d2\u7684\u89c6\u9891\u7247\u6bb5<\/strong><\/h2>\n<p>video_clip_sub = video_clip.subclip(0, 10)<\/p>\n<p>video_clip_sub.write_videofile(&quot;output_subclip.mp4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528OpenCV\u8fdb\u884c\u89c6\u9891\u5e27\u63d0\u53d6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6<\/strong><\/h2>\n<p>video_path = &quot;path\/to\/your\/video.mp4&quot;<\/p>\n<p>video_capture = cv2.VideoCapture(video_path)<\/p>\n<h2><strong>\u63d0\u53d6\u5e76\u4fdd\u5b58\u7b2c\u4e00\u5e27<\/strong><\/h2>\n<p>success, frame = video_capture.read()<\/p>\n<p>if success:<\/p>\n<p>    cv2.imwrite(&quot;output_frame.jpg&quot;, frame)<\/p>\n<h2><strong>\u91ca\u653e\u89c6\u9891\u6355\u6349\u5bf9\u8c61<\/strong><\/h2>\n<p>video_capture.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528FFmpeg-python\u8fdb\u884c\u89c6\u9891\u683c\u5f0f\u8f6c\u6362<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import ffmpeg<\/p>\n<h2><strong>\u52a0\u8f7d\u5e76\u8f6c\u6362\u89c6\u9891\u6587\u4ef6\u683c\u5f0f<\/strong><\/h2>\n<p>input_video = &quot;path\/to\/your\/video.mp4&quot;<\/p>\n<p>output_video = &quot;output_video.avi&quot;<\/p>\n<p>ffmpeg.input(input_video).output(output_video).run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u53ef\u4ee5\u4e86\u89e3\u5230\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff0c\u5e76\u901a\u8fc7\u4e0d\u540c\u7684\u5e93\u5b9e\u73b0\u8fd9\u4e00\u4efb\u52a1\u3002\u4e0e\u6b64\u540c\u65f6\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u89c6\u9891\u5904\u7406\u7684\u6269\u5c55\u64cd\u4f5c\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u5e93\u8fdb\u884c\u89c6\u9891\u526a\u8f91\u3001\u5e27\u63d0\u53d6\u548c\u683c\u5f0f\u8f6c\u6362\u7b49\u64cd\u4f5c\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u8fdb\u884c\u89c6\u9891\u5904\u7406\uff0c\u6ee1\u8db3\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662fMoviePy\u3001OpenCV\u8fd8\u662fFFmpeg-python\uff0c\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u5e94\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u89c6\u9891\u65f6\u957f\u83b7\u53d6\u548c\u57fa\u672c\u7684\u89c6\u9891\u7f16\u8f91\u64cd\u4f5c\uff0cMoviePy\u662f\u4e00\u4e2a\u975e\u5e38\u53cb\u597d\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u64cd\u4f5c\u7684\u9700\u6c42\uff0cOpenCV\u5219\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u7075\u6d3b\u5904\u7406\u5404\u79cd\u591a\u5a92\u4f53\u6587\u4ef6\u548c\u683c\u5f0f\u8f6c\u6362\u7684\u9700\u6c42\uff0cFFmpeg-python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6df1\u5165\u5b66\u4e60\u548c\u5b9e\u8df5\u8fd9\u4e9b\u5e93\u7684\u4f7f\u7528\uff0c\u8bfb\u8005\u53ef\u4ee5\u4e0d\u65ad\u63d0\u5347\u81ea\u5df1\u5728\u89c6\u9891\u5904\u7406\u9886\u57df\u7684\u6280\u80fd\uff0c\u7075\u6d3b\u5e94\u5bf9\u5404\u79cd\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u4e3a\u8bfb\u8005\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u548c\u53c2\u8003\uff0c\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u5730\u5229\u7528Python\u8fdb\u884c\u89c6\u9891\u5904\u7406\u548c\u76f8\u5173\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u83b7\u53d6\u89c6\u9891\u65f6\u957f\uff0c\u4f8b\u5982OpenCV\u3001MoviePy\u6216FFmpeg\u3002MoviePy\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u5e93\uff0c\u80fd\u591f\u8f7b\u677e\u63d0\u53d6\u89c6\u9891\u4fe1\u606f\u3002\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u89c6\u9891\u7684\u65f6\u957f\uff1a<\/p>\n<pre><code class=\"language-python\">from moviepy.editor import VideoFileClip\n\nvideo = VideoFileClip(&quot;\u4f60\u7684\u6587\u4ef6\u8def\u5f84.mp4&quot;)\nduration = video.duration  # \u65f6\u957f\u4ee5\u79d2\u4e3a\u5355\u4f4d\nprint(f&quot;\u89c6\u9891\u65f6\u957f: {duration}\u79d2&quot;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86MoviePy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install moviepy<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u4f7f\u7528OpenCV\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>OpenCV\u540c\u6837\u80fd\u591f\u83b7\u53d6\u89c6\u9891\u7684\u65f6\u957f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\ncap = cv2.VideoCapture(&quot;\u4f60\u7684\u6587\u4ef6\u8def\u5f84.mp4&quot;)\nfps = cap.get(cv2.CAP_PROP_FPS)  # \u83b7\u53d6\u89c6\u9891\u5e27\u7387\nframe_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)  # \u83b7\u53d6\u603b\u5e27\u6570\nduration = frame_count \/ fps  # \u8ba1\u7b97\u65f6\u957f\nprint(f&quot;\u89c6\u9891\u65f6\u957f: {duration}\u79d2&quot;)\ncap.release()\n<\/code><\/pre>\n<p>\u786e\u4fddOpenCV\u5e93\u5df2\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install opencv-python<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\u89c6\u9891\u6587\u4ef6\u8def\u5f84\u9519\u8bef\u3001\u7f3a\u5c11\u5fc5\u8981\u7684\u5e93\u6216\u4e0d\u652f\u6301\u7684\u89c6\u9891\u683c\u5f0f\u3002\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\u4e14\u89c6\u9891\u683c\u5f0f\u53d7\u652f\u6301\uff0c\u6b64\u5916\uff0c\u68c0\u67e5\u5e93\u662f\u5426\u5df2\u6b63\u786e\u5b89\u88c5\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u5982\u6709\u9700\u8981\uff0c\u53ef\u4ee5\u67e5\u770b\u76f8\u5173\u6587\u6863\u4ee5\u83b7\u53d6\u66f4\u591a\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528MoviePy\u3001OpenCV\u3001FFmpeg\u7b49\u5e93\uff0cMovieP [&hellip;]","protected":false},"author":3,"featured_media":1027304,"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\/1027295"}],"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=1027295"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1027295\/revisions"}],"predecessor-version":[{"id":1027307,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1027295\/revisions\/1027307"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1027304"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1027295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1027295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1027295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}