将视频m4s文件转换为mp4格式
一般情况:偏大的文件为视频,偏小的文件为音频。
环境要求:下载并安装ffmpeg,并配置好环境变量,如下图:
转换代码:
import subprocessdef merge_m4s_to_mp4(video_path, audio_path, output_path):"""使用 FFmpeg 将 m4s 视频和音频流合并为 MP4 文件。:param video_path: 视频 m4s 文件路径:param audio_path: 音频 m4s 文件路径:param output_path: 输出的 mp4 文件路径"""command = ["ffmpeg", "-y", "-i", video_path, "-i", audio_path,"-c:v", "copy", "-c:a", "aac", "-strict", "experimental", output_path]try:subprocess.run(command, check=True)print(f"转换完成: {output_path}")except subprocess.CalledProcessError as e:print(f"转换失败: {e}")if __name__ == "__main__":# 示例:手动指定文件路径 video_path='1452544206/1452544206-1-30080.m4s',audio_path='1452544206/1452544206-1-30280.m4s'# 示例调用video_file = video_pathaudio_file = audio_pathoutput_file = "output222.mp4"merge_m4s_to_mp4(video_file, audio_file, output_file)
在上述操作前,应先将文件头的0字符串都删除,可用notepad++操作,或用python执行如下代码:
import os def remove_leading_zeros(file_path, save_path=None, backup=True):"""删除文件头部全部连续的字符'0':param file_path: 文件路径 :param save_path: 保存路径(默认覆盖原文件):param backup: 是否创建备份 """# 备份机制 if backup:backup_path = file_path + ".bak"os.rename(file_path, backup_path)file_path = backup_path # 读取二进制内容 with open(file_path, 'rb') as f:content = f.read() # 动态检测前导0的数量 zero_count = 0 for byte in content:if byte == 0x30: # ASCII字符'0'的十六进制值 zero_count += 1 else:break # 遇到非0字符立即终止 # 截取有效内容 new_content = content[zero_count:]# 保存处理后的文件 save_path = save_path or file_path.replace(".bak", "")with open(save_path, 'wb') as f:f.write(new_content) print(f"已删除头部连续{zero_count}个0")if __name__ == "__main__":# 单文件处理示例 remove_leading_zeros("1476061291/1476061291-1-30280.m4s")remove_leading_zeros("1476061291/1476061291-1-100050.m4s")