程式碼:
import subprocess def replace_audio_with_ffmpeg(video_path, audio_path, output_path): # 構建ffmpeg命令 command = [ 'ffmpeg', '-i', video_path, # 輸入影片檔案 '-an', # 禁用輸入影片的音訊 '-i', audio_path, # 輸入新的音訊檔案 '-c:v', 'copy', # 複製影片流 '-c:a', 'aac', # 使用AAC編碼音訊 '-strict', '-2', # 允許使用實驗性編碼器 '-y', # 覆蓋輸出檔案 output_path # 輸出檔案 ] # 執行命令 try: subprocess.run(command, check=True) print(f"Video with new audio saved to {output_path}") except subprocess.CalledProcessError as e: print(f"An error occurred: {e}") if __name__ == "__main__": file_name = r"C:\caijian\美洲豹\原" # 舊影片檔案路徑 video_path = r'%s\01毛髮知識與Psystem基礎.mp4'% file_name # 新音訊檔案路徑 audio_path = r'%s\zh-cn-1.m4a'% file_name # 輸出影片檔案路徑 output_path = r'%s_new.mp4'% video_path replace_audio_with_ffmpeg(video_path, audio_path, output_path)