File size: 2,180 Bytes
bb65ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53eab71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import subprocess

def add_silence_to_audio_ffmpeg(audio_path, tmp_audio_path, silence_duration_s=0.5):
    # 使用 ffmpeg 命令在音频前加上静音
    cmd = [
        'ffmpeg', 
        '-i', audio_path,  # 输入音频文件路径
        '-f', 'lavfi',  # 使用 lavfi 虚拟输入设备生成静音
        '-t', str(silence_duration_s),  # 静音时长,单位秒
        '-i', 'anullsrc=r=16000:cl=stereo',  # 创建静音片段(假设音频为 stereo,采样率 44100)
        '-filter_complex', '[1][0]concat=n=2:v=0:a=1[out]',  # 合并静音和原音频
        '-map', '[out]',  # 输出合并后的音频
        '-y', tmp_audio_path,  # 输出文件路径
        '-loglevel', 'error'
    ]
    
    try:
        subprocess.run(cmd, check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        raise RuntimeError(f"ffmpeg failed ({e.returncode}): {e.stderr.strip()}")
    
def convert_video_to_15fps_ffmpeg(video_path, output_path=None):
    """

    Convert a video to 15 FPS using ffmpeg.



    Parameters

    ----------

    video_path : str

        Path to the input .mp4 video.

    output_path : str, optional

        Path for the output video. If None, a new file will be created next to the input.



    Returns

    -------

    str

        The output video path.

    """

    if not os.path.exists(video_path):
        raise FileNotFoundError(f"Input video not found: {video_path}")

    # Auto-generate output path if not provided
    if output_path is None:
        base, ext = os.path.splitext(video_path)
        output_path = base + "_15fps.mp4"

    cmd = [
        "ffmpeg",
        "-i", video_path,
        "-filter:v", "fps=15",  # Set frame rate
        "-c:a", "copy",         # Copy audio without re-encoding
        "-y", output_path,      # Overwrite output
        "-loglevel", "error"
    ]

    try:
        subprocess.run(cmd, check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        raise RuntimeError(f"ffmpeg failed ({e.returncode}): {e.stderr.strip()}")

    return output_path