Communitygithub.com

iamaanahmad/moviepy-python-video

使用純 Python 搭配 moviepy 2.x 來合成與編輯影片——在素材上疊加文字、下三分之一標題、字幕,裁切與合併片段、加入音訊,建置無需 Node.js 的簡短單檔 build.py 影片。適合需要基於 Python 的影片編輯或對 AI 生成影片疊加確定性文字的情境使用。

moviepy-python-video 是什麼?

此技能讓 AI 助理能透過純 Python 程式碼直接撰寫、編輯與合成影片,完全依賴 moviepy 2.x 與 Pillow,無需安裝 Node.js 或額外工具鏈。使用者只需要提供單一 `build.py` 檔案,即可實現文字疊加、音訊合成、片段拼接、淡入淡出效果、Ken Burns 動態運鏡等常見影片編輯功能。支援 Windows、macOS 與 Linux 平台,特別適合需要確定性文字輸出(例如為 AI 生成影片加上字幕或講者姓名)或批次處理大量檔案的場景。API 設計採用 chainable 風格(如 `with_start`、`with_duration`、`resized`),與過往 moviepy 1.x 語法不同,且 `TextClip` 需指定真實字型檔案路徑。此技能也提供多張圖片配合獨立音軌的語音主導編輯模式,有助於快速產出說明型或簡報型影片。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/iamaanahmad/agents-video-editing-skills/tree/HEAD/skills/moviepy-python-video

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

moviepy(Python 影片合成)

最適合簡短、腳本化的影片,以及將精確文字疊加在素材上。若需要長時間、設計密集、可重複使用的影片,則建議使用 Remotion。

使用 moviepy 時機使用 Remotion 時機
專案是單一 Python 腳本需要 React 元件與設計系統
在現有或 AI 生成的片段上疊加文字複雜的動畫圖形與圖表
對多個檔案進行批次編輯需仰賴熱重載預覽
沒有 Node 工具鏈可用長時間、範本化的影片

設定

pip install "moviepy>=2" pillow
# 或:uv add moviepy pillow

此技能以 moviepy 2.x 為目標。其 API 與 1.x 不同:import 直接來自 moviepy,方法為 with_*/subclipped/resized,而非 set_*/subclip/resize。

基本用法

from moviepy import (VideoFileClip, AudioFileClip, ImageClip, TextClip,
                     CompositeVideoClip, CompositeAudioClip, concatenate_videoclips, vfx, afx)

clip = VideoFileClip("in.mp4").subclipped(5, 20)           # 15 秒片段
clip = clip.resized(height=1080)                            # 保持比例
clip = clip.with_effects([vfx.FadeIn(0.5), vfx.FadeOut(0.5)])

joined = concatenate_videoclips([clip, VideoFileClip("b.mp4")], method="compose")
joined.write_videofile("out/result.mp4", fps=30, codec="libx264", audio_codec="aac")

在長腳本中,請務必對 clip 呼叫 .close(),或將其用在 with 區塊內,以釋放檔案控制代碼。

文字疊加(素材上的確定性文字)

title = (TextClip(font="C:/Windows/Fonts/arial.ttf",     # 真實字型檔案路徑
                  text="Dr. Jane Doe\nChief Scientist",
                  font_size=56, color="white",
                  stroke_color="black", stroke_width=2,
                  method="caption", size=(900, None))
         .with_start(1.0).with_duration(4.0)
         .with_position(("left", 860))
         .with_effects([vfx.CrossFadeIn(0.3), vfx.CrossFadeOut(0.3)]))

final = CompositeVideoClip([clip, title])

注意事項:

  • TextClip 需要字型檔案路徑(TTF/OTF)。請將品牌字型一併放入專案中。
  • 片段時間單位為秒,相對於複合片段的起始點。
  • 如需樣式豐富的文字(圓角藥丸、漸層),請用 Pillow 渲染 PNG 後改為使用 ImageClip。
  • 文字請保持在安全區域內:距離各邊緣至少 5%。

音訊

voice = AudioFileClip("voiceover.mp3")
music = AudioFileClip("music.mp3").with_effects([afx.MultiplyVolume(0.12), afx.AudioFadeOut(2)])
music = music.with_duration(voice.duration + 1)
final = clip.with_audio(CompositeAudioClip([music, voice.with_start(0.5)]))

讓旁白決定時間軸:將片段長度設為 voice.duration + padding,而非反過來做。

音訊錨定模式

scenes = [("scene1.mp3", "intro.png"), ("scene2.mp3", "chart.png")]
parts = []
for audio_path, image_path in scenes:
    a = AudioFileClip(audio_path)
    v = ImageClip(image_path).with_duration(a.duration + 0.6).with_audio(a)
    parts.append(v)
concatenate_videoclips(parts).write_videofile("out/explainer.mp4", fps=30)

靜態畫面的 Ken Burns 效果

img = ImageClip("photo.jpg").with_duration(6)
img = img.resized(lambda t: 1.0 + 0.04 * t).with_position("center")
CompositeVideoClip([img], size=(1920, 1080)).write_videofile("out/kb.mp4", fps=30)

效能

  • 預覽時使用 fps=12 與 preset="ultrafast";最終輸出時使用 30 fps。
  • write_videofile(..., threads=4)。
  • 避免在長片段中使用逐幀 Python lambda。盡可能先透過 FFmpeg 進行預處理。

常見問題

問題原因與解決方法
ImportError: cannot import name 'editor'moviepy 2 已移除 moviepy.editor;請從 moviepy 直接匯入
AttributeError: set_duration請改用 with_duration;with_start、with_position 同樣適用
文字片段崩潰請傳入真實字型檔案路徑
拼接後音訊不同步確保所有片段共用相同 fps;音訊請在串接後才套用
渲染緩慢草稿時降低解析度;避免使用大型 resized lambda

Individual skills in this repo

This repo contains 6 individual skills — each has its own dedicated page.

iamaanahmad/captions-subtitles

Create, edit, translate and burn in captions and subtitles - transcribe speech with Whisper, write SRT/VTT/ASS files, style them, and hard-burn word-highlight captions for Shorts, Reels and TikTok. Use for any captioning, subtitle, transcript or accessibility request.

iamaanahmad/ffmpeg-essentials

Core FFmpeg and ffprobe recipes for trimming, joining, converting, resizing, compressing, speed changes, fades, audio extraction and GIF conversion. Use for any direct media transformation task, or when a video will not play, has odd dimensions, or is out of sync.

iamaanahmad/motion-design-craft

Motion design rules for programmatic and edited video - durations, easing, springs, staggering, read time for on-screen text, beat grids, transitions and visual hierarchy, plus the defaults that make a video look AI-generated. Use when animating scenes, choosing timings, or reviewing a video that feels off.

iamaanahmad/remotion-programmatic-video

Build videos as React code with Remotion - compositions, sequences, springs, interpolation, audio sync, data-driven props, studio preview and rendering to MP4. Use for explainers, product demos, release notes, data charts, branded templates, or any video that should be reproducible from code.

iamaanahmad/screen-recording-demos

Record clean product and web app demos - scripted browser recording with Playwright, cursor and zoom polish, speed ramps, trimming dead time and syncing to narration. Use when the user wants a walkthrough, tutorial, UI demo or release video of a web app, or a repeatable recording.

iamaanahmad/voiceover-tts

Generate and time narration with text-to-speech (ElevenLabs, OpenAI TTS, Kokoro, Piper) or prepare a recorded voice - script prep, per-scene audio files, pacing, pronunciation, cleanup and loudness. Use when the user needs a voiceover, narration, dubbing, or voice cloning guidance.

相關技能