Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions office/api/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,16 @@ def txt2mp3(content='程序员晚枫', file=None, mp3=r'./程序员晚枫.mp3',
None
"""
return povideo.txt2mp3(content=content, file=file, mp3=mp3, speak=speak)


from office.lib.video.video_edit_service import (
_parse_time,
cut_video,
cut_audio,
crop_video,
concat_videos,
concat_audios,
add_audio_to_video,
)


2 changes: 2 additions & 0 deletions office/lib/video/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
"""Video lib service package."""
203 changes: 203 additions & 0 deletions office/lib/video/video_edit_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# -*- coding: utf-8 -*-
"""Video and Audio editing services.
音视频剪辑服务模块(截取、画面裁剪、音视频拼接、合成/替换音频)。
"""

import os
from moviepy import VideoFileClip, AudioFileClip, concatenate_videoclips, concatenate_audioclips


def _parse_time(t):
if t is None:
return None
if isinstance(t, (int, float)):
return float(t)
if isinstance(t, str):
t = t.strip()
parts = t.split(':')
if len(parts) == 1:
return float(parts[0])
elif len(parts) == 2:
return float(parts[0]) * 60 + float(parts[1])
elif len(parts) == 3:
return float(parts[0]) * 3600 + float(parts[1]) * 60 + float(parts[2])
return float(t)


def cut_video(video_path, start_time=0, end_time=None, output_path=r'./', output_name=None):
"""Cut / trim a video file to specified time range.
剪辑/截取视频指定时间段。
Args:
video_path (str): video file path / 视频文件路径
start_time (float|str, optional): start time in seconds or "HH:MM:SS" / 开始时间。Default / 默认: 0
end_time (float|str, optional): end time in seconds or "HH:MM:SS" / 结束时间。Default / 默认: None
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
output_name (str, optional): output filename / 输出文件名。Default / 默认: None
"""
os.makedirs(output_path, exist_ok=True)
if not output_name:
base_name = os.path.basename(video_path)
name, ext = os.path.splitext(base_name)
output_name = f"{name}_cut{ext if ext else '.mp4'}"
out_file = os.path.join(output_path, output_name)

st = _parse_time(start_time)
et = _parse_time(end_time)

clip = VideoFileClip(video_path)
try:
if hasattr(clip, 'subclipped'):
sub = clip.subclipped(st, et)
else:
sub = clip.subclip(st, et)
sub.write_videofile(out_file)
finally:
clip.close()


def cut_audio(audio_path, start_time=0, end_time=None, output_path=r'./', output_name=None):
"""Cut / trim an audio file to specified time range.
剪辑/截取音频指定时间段。
Args:
audio_path (str): audio file path / 音频文件路径
start_time (float|str, optional): start time in seconds or "HH:MM:SS" / 开始时间。Default / 默认: 0
end_time (float|str, optional): end time in seconds or "HH:MM:SS" / 结束时间。Default / 默认: None
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
output_name (str, optional): output filename / 输出文件名。Default / 默认: None
"""
os.makedirs(output_path, exist_ok=True)
if not output_name:
base_name = os.path.basename(audio_path)
name, ext = os.path.splitext(base_name)
output_name = f"{name}_cut{ext if ext else '.mp3'}"
out_file = os.path.join(output_path, output_name)

st = _parse_time(start_time)
et = _parse_time(end_time)

clip = AudioFileClip(audio_path)
try:
if hasattr(clip, 'subclipped'):
sub = clip.subclipped(st, et)
else:
sub = clip.subclip(st, et)
sub.write_audiofile(out_file)
finally:
clip.close()


def crop_video(video_path, x1=0, y1=0, x2=None, y2=None, width=None, height=None, output_path=r'./', output_name=None):
"""Crop video screen region.
裁剪视频画面坐标和尺寸。
Args:
video_path (str): video file path / 视频文件路径
x1 (int, optional): left x coordinate / 左上角 x 坐标。Default / 默认: 0
y1 (int, optional): top y coordinate / 左上角 y 坐标。Default / 默认: 0
x2 (int, optional): right x coordinate / 右下角 x 坐标。Default / 默认: None
y2 (int, optional): bottom y coordinate / 右下角 y 坐标。Default / 默认: None
width (int, optional): crop width / 裁剪宽度。Default / 默认: None
height (int, optional): crop height / 裁剪高度。Default / 默认: None
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
output_name (str, optional): output filename / 输出文件名。Default / 默认: None
"""
os.makedirs(output_path, exist_ok=True)
if not output_name:
base_name = os.path.basename(video_path)
name, ext = os.path.splitext(base_name)
output_name = f"{name}_cropped{ext if ext else '.mp4'}"
out_file = os.path.join(output_path, output_name)

clip = VideoFileClip(video_path)
try:
if hasattr(clip, 'cropped'):
cropped = clip.cropped(x1=x1, y1=y1, x2=x2, y2=y2, width=width, height=height)
elif hasattr(clip, 'crop'):
cropped = clip.crop(x1=x1, y1=y1, x2=x2, y2=y2, width=width, height=height)
else:
import moviepy.video.fx.all as vfx
cropped = vfx.crop(clip, x1=x1, y1=y1, x2=x2, y2=y2, width=width, height=height)
cropped.write_videofile(out_file)
finally:
clip.close()


def concat_videos(video_list, output_path=r'./', output_name=r'concat_video.mp4'):
"""Concatenate multiple video files in sequence.
多个视频顺序拼接合并为一个视频文件。
Args:
video_list (list[str]): list of video file paths / 视频文件路径列表
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
output_name (str, optional): output filename / 输出文件名。Default / 默认: 'concat_video.mp4'
"""
os.makedirs(output_path, exist_ok=True)
out_file = os.path.join(output_path, output_name)

clips = [VideoFileClip(v) for v in video_list]
try:
final_clip = concatenate_videoclips(clips)
final_clip.write_videofile(out_file)
finally:
for c in clips:
c.close()


def concat_audios(audio_list, output_path=r'./', output_name=r'concat_audio.mp3'):
"""Concatenate multiple audio files in sequence.
多个音频顺序拼接合并为一个音频文件。
Args:
audio_list (list[str]): list of audio file paths / 音频文件路径列表
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
output_name (str, optional): output filename / 输出文件名。Default / 默认: 'concat_audio.mp3'
"""
os.makedirs(output_path, exist_ok=True)
out_file = os.path.join(output_path, output_name)

clips = [AudioFileClip(a) for a in audio_list]
try:
final_clip = concatenate_audioclips(clips)
final_clip.write_audiofile(out_file)
finally:
for c in clips:
c.close()


def add_audio_to_video(video_path, audio_path, output_path=r'./', output_name=None):
"""Add or replace audio track in a video file.
将音频合成/替换到视频文件中。
Args:
video_path (str): video file path / 视频文件路径
audio_path (str): audio file path / 音频文件路径
output_path (str, optional): output directory / 输出目录。Default / 默认: './'
output_name (str, optional): output filename / 输出文件名。Default / 默认: None
"""
os.makedirs(output_path, exist_ok=True)
if not output_name:
base_name = os.path.basename(video_path)
name, ext = os.path.splitext(base_name)
output_name = f"{name}_with_audio{ext if ext else '.mp4'}"
out_file = os.path.join(output_path, output_name)

video_clip = VideoFileClip(video_path)
audio_clip = AudioFileClip(audio_path)
try:
if hasattr(video_clip, 'with_audio'):
final_clip = video_clip.with_audio(audio_clip)
else:
final_clip = video_clip.set_audio(audio_clip)
final_clip.write_videofile(out_file)
finally:
video_clip.close()
audio_clip.close()
3 changes: 3 additions & 0 deletions skills/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ add_watermark(file='test.png', mark='python-office')
| `audio2txt` | 音频提取文字 | `office/skills/video/audio2txt/` |
| `mark2video` | 给视频添加水印 | `office/skills/video/mark2video/` |
| `txt2mp3` | 文本转语音 | `office/skills/video/txt2mp3/` |
| `video_edit` | 音频与视频剪辑集合(截取、画面裁剪、音视频拼接、合成/替换音频) | `office/skills/video/video_edit/` |



---

Expand Down
17 changes: 17 additions & 0 deletions skills/video/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@
from .audio2txt import audio2txt
from .mark2video import mark2video
from .txt2mp3 import txt2mp3
from .video_edit import (
cut_video,
cut_audio,
crop_video,
concat_videos,
concat_audios,
add_audio_to_video,
)

__all__ = [
'video2mp3',
'audio2txt',
'mark2video',
'txt2mp3',
'cut_video',
'cut_audio',
'crop_video',
'concat_videos',
'concat_audios',
'add_audio_to_video',
]



119 changes: 119 additions & 0 deletions skills/video/video_edit/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
name: video_edit
description: 音视频剪辑工具箱。包含视频剪辑截取、音频剪辑截取、视频画面裁剪、视频拼接合并、音频拼接合并、视频合成/替换音频。当用户提到音视频剪辑、裁剪视频、合并视频、音频剪辑、视频配音时使用。
---

# video_edit Skill

> 音视频剪辑功能集(截取、裁剪、拼接、配音/音轨替换)
## 功能描述

本 Skill 将所有音视频剪辑相关功能合并为一个统一的 API 模块,包含以下 6 个核心剪辑功能:

1. `cut_video`: 截取视频指定时间段片段
2. `cut_audio`: 截取音频指定时间段片段
3. `crop_video`: 裁剪视频画面的坐标区域与尺寸
4. `concat_videos`: 多个视频文件顺序拼接合并
5. `concat_audios`: 多个音频文件顺序拼接合并
6. `add_audio_to_video`: 视频合成/替换背景音乐或音频

## 所属分类

`office/skills/video/video_edit/`

## 调用方式

```python
from skills.video.video_edit import (
cut_video,
cut_audio,
crop_video,
concat_videos,
concat_audios,
add_audio_to_video,
)
```

或从 `skills.video` 模块导入:
```python
from skills.video import video_edit

video_edit.cut_video(video_path='./test.mp4', start_time='00:00:10', end_time='00:00:30')
```

## 功能与参数说明

### 1. 视频截取 `cut_video`
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|------|------|------|--------|------|
| `video_path` | str | 是 | - | 视频文件路径 |
| `start_time` | float/str | 否 | `0` | 开始时间(秒数或格式如 `'00:01:10'`) |
| `end_time` | float/str | 否 | `None` | 结束时间(秒数或格式如 `'00:02:30'`,默认结尾) |
| `output_path` | str | 否 | `'./'` | 输出目录 |
| `output_name` | str | 否 | `None` | 输出文件名(默认原名后加 `_cut`) |

### 2. 音频截取 `cut_audio`
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|------|------|------|--------|------|
| `audio_path` | str | 是 | - | 音频文件路径 |
| `start_time` | float/str | 否 | `0` | 开始时间(秒数或格式如 `'00:00:05'`) |
| `end_time` | float/str | 否 | `None` | 结束时间 |
| `output_path` | str | 否 | `'./'` | 输出目录 |
| `output_name` | str | 否 | `None` | 输出文件名 |

### 3. 画面裁剪 `crop_video`
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|------|------|------|--------|------|
| `video_path` | str | 是 | - | 视频文件路径 |
| `x1` | int | 否 | `0` | 左上角 x 坐标 |
| `y1` | int | 否 | `0` | 左上角 y 坐标 |
| `x2` | int | 否 | `None` | 右下角 x 坐标 |
| `y2` | int | 否 | `None` | 右下角 y 坐标 |
| `width` | int | 否 | `None` | 裁剪宽度(可代替 x2) |
| `height` | int | 否 | `None` | 裁剪高度(可代替 y2) |
| `output_path` | str | 否 | `'./'` | 输出目录 |
| `output_name` | str | 否 | `None` | 输出文件名 |

### 4. 视频拼接 `concat_videos`
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|------|------|------|--------|------|
| `video_list` | list[str] | 是 | - | 视频路径列表 |
| `output_path` | str | 否 | `'./'` | 输出目录 |
| `output_name` | str | 否 | `'concat_video.mp4'` | 输出文件名 |

### 5. 音频拼接 `concat_audios`
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|------|------|------|--------|------|
| `audio_list` | list[str] | 是 | - | 音频路径列表 |
| `output_path` | str | 否 | `'./'` | 输出目录 |
| `output_name` | str | 否 | `'concat_audio.mp3'` | 输出文件名 |

### 6. 合成音频/配音 `add_audio_to_video`
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|------|------|------|--------|------|
| `video_path` | str | 是 | - | 视频文件路径 |
| `audio_path` | str | 是 | - | 音频文件路径 |
| `output_path` | str | 否 | `'./'` | 输出目录 |
| `output_name` | str | 否 | `None` | 输出文件名 |

## 使用示例

```python
from skills.video import video_edit

# 视频截取
video_edit.cut_video(video_path='./input.mp4', start_time=0, end_time=10)

# 视频合成音频
video_edit.add_audio_to_video(video_path='./input.mp4', audio_path='./music.mp3')
```

## 原始函数

- `office.api.video.cut_video`
- `office.api.video.cut_audio`
- `office.api.video.crop_video`
- `office.api.video.concat_videos`
- `office.api.video.concat_audios`
- `office.api.video.add_audio_to_video`
Loading