ffmpeg简单使用(1)-转码、添加字幕 发表于 2019-04-30 | 分类于 Multimedia | h.264 转 h.2651$ ffmpeg -i source [H264video] -i -c:v libx265 [outputVideo] e.g. 1$ ffmpeg -i billions.s01e02.1080p.bluray.x264.mkv -c:v libx265 billions.s01e02.1080p.bluray.x265.mkv 添加字幕1$ ffmpeg -i source Video.mkv -i source Subtitle.srt -c copy outputVideo.mkv e.g. 1$ ffmpeg -i billions.s01e01.1080p.bluray.x265.mkv -i Billions.S01E01.Pilot.720p.BluRay.x264.srt -c copy billions.s01e01.1080p.bluray.x265-ch.en.mkv 批量处理Mac & Linux下可以使用shell批量处理,Win下使用Python批量处理: 12345678910111213141516import osvideo_format = '.mkv'episode_num = 3path = ‘C:\Billions.S01.1080p.BluRay.x264\srt'file_list = os.listdir(path)video_list = [file for file in file_list if file.endswith(video_format)]print(video_list)for num, video in enumerate(video_list, start=episode_num): # print(num, video) # command = 'ffmpeg -i {0} -i billions.s01e0{1}.srt -c copy billions.s01e0{1}.1080p.bluray.x264-ch.en.mkv'.format(video, num) command = 'ffmpeg -i {0} -c:v libx265 billions.s01e0{1}.1080p.bluray.x265.ch.en.mkv'.format(video, num) os.system(command) 转换文件编码格式(GBK -> UTF-8): 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import osimport codecsimport chardetdef convert(file, in_enc="GBK", out_enc="UTF-8"): """ 该程序用于将目录下的文件从指定格式转换到指定格式,默认的是GBK转到utf-8 :param file: 文件路径 :param in_enc: 输入文件格式 :param out_enc: 输出文件格式 :return: """ in_enc = in_enc.upper() out_enc = out_enc.upper() try: print("convert [ " + file.split('\\')[-1] + " ].....From " + in_enc + " --> " + out_enc ) f = codecs.open(file, 'r', in_enc, errors='ignore') new_content = f.read() codecs.open(file, 'w', out_enc).write(new_content) # print (f.read()) except IOError as err: print("I/O error: {0}".format(err))def list_folders_files(path): """ 返回 "文件夹" 和 "文件" 名字 :param path: "文件夹"和"文件"所在的路径 :return: (list_folders, list_files) :list_folders: 文件夹 :list_files: 文件 """ list_folders = [] list_files = [] for item in os.listdir(path): file_path = os.path.join(path, item) if os.path.isdir(file_path): list_folders.append(item) else: list_files.append(item) return (list_folders, list_files)#将路径下面的所有文件,从原来的格式变为UTF-8的格式if __name__ == "__main__": path = r'Z:\影音\剧\Billions.S01.1080p.BluRay.x264-ROVERS[rartv]\亿万1BD-DEMAND' (list_folders, list_files) = list_folders_files(path) print("Path: " + path) for file_name in list_files: file_path = os.path.join(path, file_name) # filePath = path + '\\' + fileName with open(file_path, "rb") as f: data = f.read() encoding_type = chardet.detect(data)['encoding'] convert(file_path, encoding_type, 'UTF-8') # GB18030 欢迎您扫一扫上面的微信公众号,订阅我的博客!