upload_file.py 1.58 KB
import os
import subprocess
import requests


def download_audio(url, output_path):
    """Download audios file from URL."""
    response = requests.get(url)
    with open(output_path, 'wb') as f:
        f.write(response.content)


def cut_audio(input_path, output_base_path, start_times, durations):
    """Cut audios into multiple clips using FFmpeg."""
    if not os.path.exists(os.path.dirname(output_base_path)):
        os.makedirs(os.path.dirname(output_base_path))

    for index, (start_time, duration) in enumerate(zip(start_times, durations), 1):
        output_path = f"{output_base_path}{index}.mp3"
        cmd = [
            'ffmpeg',
            '-ss', str(start_time),
            '-i', input_path,
            '-t', str(duration),
            '-c', 'copy',
            output_path
        ]
        subprocess.run(cmd, check=True)


def main():
    # Replace these with your URLs and desired output directory
    audio_url = 'https://smartcard-1253080096.cos.ap-guangzhou.myqcloud.com/audio/806599C6309C_1710219356_806599C6309C-1710219356.MP3'
    output_directory = 'D:/software/python/project/funasr_web/results/1best_recog'

    # Time offsets and durations for each clip
    start_times = [0, 10, 20]
    durations = [10, 10, 10]

    # Download the audios file
    audio_file_name = os.path.basename(audio_url)
    audio_file_path = os.path.join(output_directory, audio_file_name)
    download_audio(audio_url, audio_file_path)

    # Cut the audios file into clips
    cut_audio(audio_file_path, output_directory + 'clip_', start_times, durations)


if __name__ == '__main__':
    main()