Skip to content

Commit 0409845

Browse files
eggCodedmbdmb-255474348
andauthored
feat(video): add unified video_edit skill for audio and video editing (CoderWanFeng#171)
Add video_edit skill combining all 6 audio and video editing features: - cut_video: trim/cut video file by start_time and end_time - cut_audio: trim/cut audio file by start_time and end_time - crop_video: crop video screen coordinates and dimensions - concat_videos: concatenate multiple video files in sequence - concat_audios: concatenate multiple audio files in sequence - add_audio_to_video: add or replace audio track in video file Service implementation added to office/lib/video/video_edit_service.py. Co-authored-by: dmb-255474348 <dmb-255474348@qq.com>
1 parent 23a4a05 commit 0409845

7 files changed

Lines changed: 376 additions & 0 deletions

File tree

‎office/api/video.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,16 @@ def txt2mp3(content='程序员晚枫', file=None, mp3=r'./程序员晚枫.mp3',
9595
None
9696
"""
9797
return povideo.txt2mp3(content=content, file=file, mp3=mp3, speak=speak)
98+
99+
100+
from office.lib.video.video_edit_service import (
101+
_parse_time,
102+
cut_video,
103+
cut_audio,
104+
crop_video,
105+
concat_videos,
106+
concat_audios,
107+
add_audio_to_video,
108+
)
109+
110+

‎office/lib/video/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: utf-8 -*-
2+
"""Video lib service package."""
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# -*- coding: utf-8 -*-
2+
"""Video and Audio editing services.
3+
4+
音视频剪辑服务模块(截取、画面裁剪、音视频拼接、合成/替换音频)。
5+
"""
6+
7+
import os
8+
from moviepy import VideoFileClip, AudioFileClip, concatenate_videoclips, concatenate_audioclips
9+
10+
11+
def _parse_time(t):
12+
if t is None:
13+
return None
14+
if isinstance(t, (int, float)):
15+
return float(t)
16+
if isinstance(t, str):
17+
t = t.strip()
18+
parts = t.split(':')
19+
if len(parts) == 1:
20+
return float(parts[0])
21+
elif len(parts) == 2:
22+
return float(parts[0]) * 60 + float(parts[1])
23+
elif len(parts) == 3:
24+
return float(parts[0]) * 3600 + float(parts[1]) * 60 + float(parts[2])
25+
return float(t)
26+
27+
28+
def cut_video(video_path, start_time=0, end_time=None, output_path=r'./', output_name=None):
29+
"""Cut / trim a video file to specified time range.
30+
31+
剪辑/截取视频指定时间段。
32+
33+
Args:
34+
video_path (str): video file path / 视频文件路径
35+
start_time (float|str, optional): start time in seconds or "HH:MM:SS" / 开始时间。Default / 默认: 0
36+
end_time (float|str, optional): end time in seconds or "HH:MM:SS" / 结束时间。Default / 默认: None
37+
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
38+
output_name (str, optional): output filename / 输出文件名。Default / 默认: None
39+
"""
40+
os.makedirs(output_path, exist_ok=True)
41+
if not output_name:
42+
base_name = os.path.basename(video_path)
43+
name, ext = os.path.splitext(base_name)
44+
output_name = f"{name}_cut{ext if ext else '.mp4'}"
45+
out_file = os.path.join(output_path, output_name)
46+
47+
st = _parse_time(start_time)
48+
et = _parse_time(end_time)
49+
50+
clip = VideoFileClip(video_path)
51+
try:
52+
if hasattr(clip, 'subclipped'):
53+
sub = clip.subclipped(st, et)
54+
else:
55+
sub = clip.subclip(st, et)
56+
sub.write_videofile(out_file)
57+
finally:
58+
clip.close()
59+
60+
61+
def cut_audio(audio_path, start_time=0, end_time=None, output_path=r'./', output_name=None):
62+
"""Cut / trim an audio file to specified time range.
63+
64+
剪辑/截取音频指定时间段。
65+
66+
Args:
67+
audio_path (str): audio file path / 音频文件路径
68+
start_time (float|str, optional): start time in seconds or "HH:MM:SS" / 开始时间。Default / 默认: 0
69+
end_time (float|str, optional): end time in seconds or "HH:MM:SS" / 结束时间。Default / 默认: None
70+
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
71+
output_name (str, optional): output filename / 输出文件名。Default / 默认: None
72+
"""
73+
os.makedirs(output_path, exist_ok=True)
74+
if not output_name:
75+
base_name = os.path.basename(audio_path)
76+
name, ext = os.path.splitext(base_name)
77+
output_name = f"{name}_cut{ext if ext else '.mp3'}"
78+
out_file = os.path.join(output_path, output_name)
79+
80+
st = _parse_time(start_time)
81+
et = _parse_time(end_time)
82+
83+
clip = AudioFileClip(audio_path)
84+
try:
85+
if hasattr(clip, 'subclipped'):
86+
sub = clip.subclipped(st, et)
87+
else:
88+
sub = clip.subclip(st, et)
89+
sub.write_audiofile(out_file)
90+
finally:
91+
clip.close()
92+
93+
94+
def crop_video(video_path, x1=0, y1=0, x2=None, y2=None, width=None, height=None, output_path=r'./', output_name=None):
95+
"""Crop video screen region.
96+
97+
裁剪视频画面坐标和尺寸。
98+
99+
Args:
100+
video_path (str): video file path / 视频文件路径
101+
x1 (int, optional): left x coordinate / 左上角 x 坐标。Default / 默认: 0
102+
y1 (int, optional): top y coordinate / 左上角 y 坐标。Default / 默认: 0
103+
x2 (int, optional): right x coordinate / 右下角 x 坐标。Default / 默认: None
104+
y2 (int, optional): bottom y coordinate / 右下角 y 坐标。Default / 默认: None
105+
width (int, optional): crop width / 裁剪宽度。Default / 默认: None
106+
height (int, optional): crop height / 裁剪高度。Default / 默认: None
107+
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
108+
output_name (str, optional): output filename / 输出文件名。Default / 默认: None
109+
"""
110+
os.makedirs(output_path, exist_ok=True)
111+
if not output_name:
112+
base_name = os.path.basename(video_path)
113+
name, ext = os.path.splitext(base_name)
114+
output_name = f"{name}_cropped{ext if ext else '.mp4'}"
115+
out_file = os.path.join(output_path, output_name)
116+
117+
clip = VideoFileClip(video_path)
118+
try:
119+
if hasattr(clip, 'cropped'):
120+
cropped = clip.cropped(x1=x1, y1=y1, x2=x2, y2=y2, width=width, height=height)
121+
elif hasattr(clip, 'crop'):
122+
cropped = clip.crop(x1=x1, y1=y1, x2=x2, y2=y2, width=width, height=height)
123+
else:
124+
import moviepy.video.fx.all as vfx
125+
cropped = vfx.crop(clip, x1=x1, y1=y1, x2=x2, y2=y2, width=width, height=height)
126+
cropped.write_videofile(out_file)
127+
finally:
128+
clip.close()
129+
130+
131+
def concat_videos(video_list, output_path=r'./', output_name=r'concat_video.mp4'):
132+
"""Concatenate multiple video files in sequence.
133+
134+
多个视频顺序拼接合并为一个视频文件。
135+
136+
Args:
137+
video_list (list[str]): list of video file paths / 视频文件路径列表
138+
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
139+
output_name (str, optional): output filename / 输出文件名。Default / 默认: 'concat_video.mp4'
140+
"""
141+
os.makedirs(output_path, exist_ok=True)
142+
out_file = os.path.join(output_path, output_name)
143+
144+
clips = [VideoFileClip(v) for v in video_list]
145+
try:
146+
final_clip = concatenate_videoclips(clips)
147+
final_clip.write_videofile(out_file)
148+
finally:
149+
for c in clips:
150+
c.close()
151+
152+
153+
def concat_audios(audio_list, output_path=r'./', output_name=r'concat_audio.mp3'):
154+
"""Concatenate multiple audio files in sequence.
155+
156+
多个音频顺序拼接合并为一个音频文件。
157+
158+
Args:
159+
audio_list (list[str]): list of audio file paths / 音频文件路径列表
160+
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
161+
output_name (str, optional): output filename / 输出文件名。Default / 默认: 'concat_audio.mp3'
162+
"""
163+
os.makedirs(output_path, exist_ok=True)
164+
out_file = os.path.join(output_path, output_name)
165+
166+
clips = [AudioFileClip(a) for a in audio_list]
167+
try:
168+
final_clip = concatenate_audioclips(clips)
169+
final_clip.write_audiofile(out_file)
170+
finally:
171+
for c in clips:
172+
c.close()
173+
174+
175+
def add_audio_to_video(video_path, audio_path, output_path=r'./', output_name=None):
176+
"""Add or replace audio track in a video file.
177+
178+
将音频合成/替换到视频文件中。
179+
180+
Args:
181+
video_path (str): video file path / 视频文件路径
182+
audio_path (str): audio file path / 音频文件路径
183+
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
184+
output_name (str, optional): output filename / 输出文件名。Default / 默认: None
185+
"""
186+
os.makedirs(output_path, exist_ok=True)
187+
if not output_name:
188+
base_name = os.path.basename(video_path)
189+
name, ext = os.path.splitext(base_name)
190+
output_name = f"{name}_with_audio{ext if ext else '.mp4'}"
191+
out_file = os.path.join(output_path, output_name)
192+
193+
video_clip = VideoFileClip(video_path)
194+
audio_clip = AudioFileClip(audio_path)
195+
try:
196+
if hasattr(video_clip, 'with_audio'):
197+
final_clip = video_clip.with_audio(audio_clip)
198+
else:
199+
final_clip = video_clip.set_audio(audio_clip)
200+
final_clip.write_videofile(out_file)
201+
finally:
202+
video_clip.close()
203+
audio_clip.close()

‎skills/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ add_watermark(file='test.png', mark='python-office')
199199
| `audio2txt` | 音频提取文字 | `office/skills/video/audio2txt/` |
200200
| `mark2video` | 给视频添加水印 | `office/skills/video/mark2video/` |
201201
| `txt2mp3` | 文本转语音 | `office/skills/video/txt2mp3/` |
202+
| `video_edit` | 音频与视频剪辑集合(截取、画面裁剪、音视频拼接、合成/替换音频) | `office/skills/video/video_edit/` |
203+
204+
202205

203206
---
204207

‎skills/video/__init__.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,27 @@
44
from .audio2txt import audio2txt
55
from .mark2video import mark2video
66
from .txt2mp3 import txt2mp3
7+
from .video_edit import (
8+
cut_video,
9+
cut_audio,
10+
crop_video,
11+
concat_videos,
12+
concat_audios,
13+
add_audio_to_video,
14+
)
715

816
__all__ = [
917
'video2mp3',
1018
'audio2txt',
1119
'mark2video',
1220
'txt2mp3',
21+
'cut_video',
22+
'cut_audio',
23+
'crop_video',
24+
'concat_videos',
25+
'concat_audios',
26+
'add_audio_to_video',
1327
]
28+
29+
30+

‎skills/video/video_edit/SKILL.md‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
name: video_edit
3+
description: 音视频剪辑工具箱。包含视频剪辑截取、音频剪辑截取、视频画面裁剪、视频拼接合并、音频拼接合并、视频合成/替换音频。当用户提到音视频剪辑、裁剪视频、合并视频、音频剪辑、视频配音时使用。
4+
---
5+
6+
# video_edit Skill
7+
8+
> 音视频剪辑功能集(截取、裁剪、拼接、配音/音轨替换)
9+
10+
## 功能描述
11+
12+
本 Skill 将所有音视频剪辑相关功能合并为一个统一的 API 模块,包含以下 6 个核心剪辑功能:
13+
14+
1. `cut_video`: 截取视频指定时间段片段
15+
2. `cut_audio`: 截取音频指定时间段片段
16+
3. `crop_video`: 裁剪视频画面的坐标区域与尺寸
17+
4. `concat_videos`: 多个视频文件顺序拼接合并
18+
5. `concat_audios`: 多个音频文件顺序拼接合并
19+
6. `add_audio_to_video`: 视频合成/替换背景音乐或音频
20+
21+
## 所属分类
22+
23+
`office/skills/video/video_edit/`
24+
25+
## 调用方式
26+
27+
```python
28+
from skills.video.video_edit import (
29+
cut_video,
30+
cut_audio,
31+
crop_video,
32+
concat_videos,
33+
concat_audios,
34+
add_audio_to_video,
35+
)
36+
```
37+
38+
或从 `skills.video` 模块导入:
39+
```python
40+
from skills.video import video_edit
41+
42+
video_edit.cut_video(video_path='./test.mp4', start_time='00:00:10', end_time='00:00:30')
43+
```
44+
45+
## 功能与参数说明
46+
47+
### 1. 视频截取 `cut_video`
48+
| 参数 | 类型 | 必填 | 默认值 | 说明 |
49+
|------|------|------|--------|------|
50+
| `video_path` | str | 是 | - | 视频文件路径 |
51+
| `start_time` | float/str | 否 | `0` | 开始时间(秒数或格式如 `'00:01:10'`) |
52+
| `end_time` | float/str | 否 | `None` | 结束时间(秒数或格式如 `'00:02:30'`,默认结尾) |
53+
| `output_path` | str | 否 | `'./'` | 输出目录 |
54+
| `output_name` | str | 否 | `None` | 输出文件名(默认原名后加 `_cut`) |
55+
56+
### 2. 音频截取 `cut_audio`
57+
| 参数 | 类型 | 必填 | 默认值 | 说明 |
58+
|------|------|------|--------|------|
59+
| `audio_path` | str | 是 | - | 音频文件路径 |
60+
| `start_time` | float/str | 否 | `0` | 开始时间(秒数或格式如 `'00:00:05'`) |
61+
| `end_time` | float/str | 否 | `None` | 结束时间 |
62+
| `output_path` | str | 否 | `'./'` | 输出目录 |
63+
| `output_name` | str | 否 | `None` | 输出文件名 |
64+
65+
### 3. 画面裁剪 `crop_video`
66+
| 参数 | 类型 | 必填 | 默认值 | 说明 |
67+
|------|------|------|--------|------|
68+
| `video_path` | str | 是 | - | 视频文件路径 |
69+
| `x1` | int | 否 | `0` | 左上角 x 坐标 |
70+
| `y1` | int | 否 | `0` | 左上角 y 坐标 |
71+
| `x2` | int | 否 | `None` | 右下角 x 坐标 |
72+
| `y2` | int | 否 | `None` | 右下角 y 坐标 |
73+
| `width` | int | 否 | `None` | 裁剪宽度(可代替 x2) |
74+
| `height` | int | 否 | `None` | 裁剪高度(可代替 y2) |
75+
| `output_path` | str | 否 | `'./'` | 输出目录 |
76+
| `output_name` | str | 否 | `None` | 输出文件名 |
77+
78+
### 4. 视频拼接 `concat_videos`
79+
| 参数 | 类型 | 必填 | 默认值 | 说明 |
80+
|------|------|------|--------|------|
81+
| `video_list` | list[str] | 是 | - | 视频路径列表 |
82+
| `output_path` | str | 否 | `'./'` | 输出目录 |
83+
| `output_name` | str | 否 | `'concat_video.mp4'` | 输出文件名 |
84+
85+
### 5. 音频拼接 `concat_audios`
86+
| 参数 | 类型 | 必填 | 默认值 | 说明 |
87+
|------|------|------|--------|------|
88+
| `audio_list` | list[str] | 是 | - | 音频路径列表 |
89+
| `output_path` | str | 否 | `'./'` | 输出目录 |
90+
| `output_name` | str | 否 | `'concat_audio.mp3'` | 输出文件名 |
91+
92+
### 6. 合成音频/配音 `add_audio_to_video`
93+
| 参数 | 类型 | 必填 | 默认值 | 说明 |
94+
|------|------|------|--------|------|
95+
| `video_path` | str | 是 | - | 视频文件路径 |
96+
| `audio_path` | str | 是 | - | 音频文件路径 |
97+
| `output_path` | str | 否 | `'./'` | 输出目录 |
98+
| `output_name` | str | 否 | `None` | 输出文件名 |
99+
100+
## 使用示例
101+
102+
```python
103+
from skills.video import video_edit
104+
105+
# 视频截取
106+
video_edit.cut_video(video_path='./input.mp4', start_time=0, end_time=10)
107+
108+
# 视频合成音频
109+
video_edit.add_audio_to_video(video_path='./input.mp4', audio_path='./music.mp3')
110+
```
111+
112+
## 原始函数
113+
114+
- `office.api.video.cut_video`
115+
- `office.api.video.cut_audio`
116+
- `office.api.video.crop_video`
117+
- `office.api.video.concat_videos`
118+
- `office.api.video.concat_audios`
119+
- `office.api.video.add_audio_to_video`

0 commit comments

Comments
 (0)