|
| 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() |
0 commit comments