python 批量ts合并成一个mp4

首先,确保你已经安装了ffmpeg

然后再次保证所有ts文件放在同一个文件夹中,并且依次命名为 1.ts 、 2.ts 、 3.ts 、 4.ts 、 4.ts 。。。

Python完整代码如下:(ffmpeg_batch_merge_ts.py文件)

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import os


# # 设置包含.ts文件的目录。
# ts_directory = '/path/to/ts/files';

# # 遍历目录中的所有.ts文件。
# for filename in os.listdir(ts_directory):
#     if filename.endswith('.ts'):
#         # 构建源文件和目标文件的路径。
#         source_path = os.path.join(ts_directory, filename);
#         target_path = os.path.join(ts_directory, os.path.splitext(filename)[0] + '.mp4');
#
#         # 构建ffmpeg命令并运行。
#         ffmpeg_command = f'ffmpeg -i "{source_path}" "{target_path}"';
#         os.system(ffmpeg_command);


def is_no_ignore(ignore_id_tuple, the_id):
    for x in ignore_id_tuple:
        if x == the_id: return False;
    return True;


# 定义函数。
def file_copy(source_path, target_path):
    print('file_copy');
    with open(source_path, "rb") as source_file:
        with open(target_path, "wb") as target_file:
            target_file.write(source_file.read());


# 设置包含.ts文件的目录。
# ts_directory = '/path/to/ts/files';
ts_directory = '';
while len(ts_directory) == 0:
    # 在Python中, 你可以使用input()函数来获取键盘输入。
    ts_directory = input('请输入ts文件所在的目录: ');
os.system(f'cd "{ts_directory}" && pwd');
# 由于ffmpeg一次合并的ts数量不能过大, 否则合并失败, 因此需要限制。
ts_number_per_group = 100;
ts_count = 0;
while ts_count <= 0:
    try:
        ts_count = int(input('请输入ts的数量: '));
    except ValueError:
        print('[Error] 您输入的数量不是整数!')
# ts_ignore_id_list = [120, 121, 122, 123, 124, 455, 456, 457, 458, 459];
# ts_ignore_id_tuple = (120, 121, 122, 123, 124, 455, 456, 457, 458, 459);
ts_ignore_id_tuple = ();
if ts_count > ts_number_per_group:
    ts_group_count = (ts_count + ts_number_per_group - 1) // ts_number_per_group;
    print('分组组数:', ts_group_count);
    final_ffmpeg_input = '';
    for group_index in range(ts_group_count):
        group_id = group_index + 1;
        print('第', group_id, '组开始');
        ts_start_id = ts_number_per_group * group_index + 1;
        ts_end_id = ts_number_per_group * (group_index + 1);
        if ts_end_id > ts_count: ts_end_id = ts_count;
        ffmpeg_input = '';
        for ts_id in range(ts_start_id, ts_end_id):
            # print(group_id, ts_id);
            if is_no_ignore(ts_ignore_id_tuple, ts_id):
                ffmpeg_input += f'{ts_id}.ts|';
            else:
                print('Ignore_id', ts_id);
        else:
            if is_no_ignore(ts_ignore_id_tuple, ts_id):
                ffmpeg_input += f'{ts_end_id}.ts';
            else:
                ffmpeg_input = ffmpeg_input[0:len(ffmpeg_input) - 1];
        print(ffmpeg_input);
        if ffmpeg_input.find('|') != -1:
            # 一个组内ts合并。
            os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{ffmpeg_input}" -c copy temp_{group_id}.mp4');
            # 把 一个组内ts合并的mp4 转成 temp.ts 。
            os.system(
                f'cd "{ts_directory}" && ffmpeg -i temp_{group_id}.mp4 -codec copy -vbsf h264_mp4toannexb temp_{group_id}.ts');
        else:
            file_copy(f'{ts_directory}/{ffmpeg_input}', f'{ts_directory}/temp_{group_id}.ts');
        final_ffmpeg_input += f'temp_{group_id}.ts|';
        print('第', group_id, '组结束');

    final_ffmpeg_input = final_ffmpeg_input[0:len(final_ffmpeg_input) - 1];
    print(final_ffmpeg_input);
    # 把 合成的组temp.ts 再次合并。
    os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{final_ffmpeg_input}" -c copy target.mp4');
else:
    print('only one group');
    ffmpeg_input = '';
    for ts_id in range(1, ts_count):
        # print(group_id, ts_id);
        if is_no_ignore(ts_ignore_id_tuple, ts_id):
            ffmpeg_input += f'{ts_id}.ts|';
        else:
            print('Ignore_id', ts_id);
    ffmpeg_input += f'{ts_count}.ts';
    print(ffmpeg_input);
    # 一个组内ts合并。
    os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{ffmpeg_input}" -c copy target.mp4');

print('track_main_exit');

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Python中,实现多张图片合并一张图片的方法有很多种,其中最简单的一种就是使用PIL库中的Image模块。以下是具体实现的步骤: 1. 导入PIL库和要用到的图片 ``` from PIL import Image img1 = Image.open("img1.png") img2 = Image.open("img2.png") img3 = Image.open("img3.png") ``` 2. 获取图片的大小信息,并计算合并后图片的大小 ``` width, height = img1.size total_width = 3 * width # 三张图片合并 total_height = height ``` 3. 创建新的空白图片,将三张图片依次粘贴到空白图片上 ``` new_img = Image.new("RGB", (total_width, total_height)) new_img.paste(img1, (0, 0)) new_img.paste(img2, (width, 0)) new_img.paste(img3, (2 * width, 0)) ``` 4. 保存合并后的图片 ``` new_img.save("merged_img.png") ``` 以上就是合并三张图片为一张图片的完整代码。如果想合并更多的图片,只需要在计算合并后图片大小和粘贴图片的过程中进行相应的修改即可。 ### 回答2: 在Python中,有多种方法可以实现多张图合并一张图的功能。这里介绍两种较为常用的方法。 方法一:Pillow库 Pillow是Python一个常用的图像处理库,它提供了丰富的图像处理功能,其中就包括多张图合并一张图的功能。 首先需要安装Pillow库,在命令行中使用以下命令进行安装: pip install pillow 接下来创建一组要合并的图像: from PIL import Image # 创建图像对象 img1 = Image.open('image1.jpg') img2 = Image.open('image2.jpg') img3 = Image.open('image3.jpg') # 获取图像宽度和高度 width, height = img1.size 然后将这组图像按照一定的规则合并一张图。比如,可以将这三张图分别放置在一行中: # 创建新图像,大小为3倍宽度,高度为原图像高度 res_img = Image.new('RGB', (width * 3, height)) # 将三张图像依次放入新图像 res_img.paste(img1, (0, 0)) res_img.paste(img2, (width, 0)) res_img.paste(img3, (width * 2, 0)) 最后将合并后的图像保存: # 保存合并后的图像 res_img.save('result.jpg') 方法二:OpenCV库 OpenCV是一个开源的计算机视觉库,主要用于图像处理和机器视觉领域。它也可以用来实现多张图合并一张图的功能。 首先需要安装OpenCV库,在命令行中使用以下命令进行安装: pip install opencv-python 接下来创建一组要合并的图像: import cv2 # 加载三张图像 img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') img3 = cv2.imread('image3.jpg') # 获取图像宽度和高度 width, height = img1.shape[:2] 然后将这组图像按照一定的规则合并一张图。比如,可以将这三张图分别放置在一行中: # 创建新图像,大小为3倍宽度,高度为原图像高度 res_img = np.zeros((height, width * 3, 3), np.uint8) # 将三张图像依次放入新图像 res_img[:, :width] = img1 res_img[:, width:width*2] = img2 res_img[:, width*2:] = img3 最后将合并后的图像保存: # 保存合并后的图像 cv2.imwrite('result.jpg', res_img) 总的来说,无论是使用Pillow库还是OpenCV库,都能够轻松实现多张图合并一张图的功能。根据具体的需求,可以选择不同的方法和算法来实现。 ### 回答3: Python是一种高级编程语言,用于编写功能强大的程序和自动化脚本。当涉及到对图像进行操作时,Python有很多内置的模块和库,使其为一种优秀的图像处理语言。在Python中,有多种方法可以将多张图像合并一张图像。 Python中有很多在处理图像时非常有效的第三方库,其中最流行的就是Pillow。在使用Pillow中的Image模块时,可以通过操作Image对象来合并多张图像。 首先,使用Pillow的Image模块加载所有需要合并的图像,这样就可以使用它们的像素数据。然后,可以创建一个名称为canvas的空白Image对象,并指定其大小,这将为最终的合并图像。接下来,可以使用paste方法将每个原始图像粘贴到canvas对象上,然后保存新图像。 以下是一个简单的例子,展示了如何使用Pillow合并多张图像: ```python from PIL import Image #打开需要合并的图像 img1 = Image.open('image1.jpg') img2 = Image.open('image2.jpg') img3 = Image.open('image3.jpg') #创建空白画布 canvas = Image.new('RGB', (1000, 1000), 'white') #将每张图像粘贴到画布上 canvas.paste(img1, (0, 0)) canvas.paste(img2, (200, 500)) canvas.paste(img3, (400, 0)) #保存合并后的图像 canvas.save('combined_image.jpg') ``` 在本例中,通过创建空白的1000 x 1000像素的白色画布作为合并后的图像。接下来,将每个打开的原始图像粘贴到画布上,位置可以根据需要自己指定。最后,使用保存方法将新图像保存到磁盘。 简单来说,合并多张图像并不难,只需要使用Pillow或其他Python库中的图像处理模块即可。 主要包括三个步骤,第一步要打开每张需要合并的图像,第二步是创建一个空白的画布或图像,第三步是通过paste方法将每个原始图像粘贴到画布上,并保存新的合并后的图像。这是一个简单但非常强大的技巧,可以用于许多图像处理和图像合的场合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值