ffmpeg合併影片

szd426發表於2024-08-31

安裝ffmpeg命令工具:

1.下載ffmpeg工具 【官網

2.如果需要下載 7-zip解壓該檔案【官網

3.解壓後重名解壓資料夾為ffmpeg

4.複製ffmpeg資料夾到 "C:\Program Files"

5.新增系統環境變數 “C:\Program Files\ffmpeg\bin”

6.可以透過 ffmpeg 檢視


7. 對於需要合併的ts 參考如下程式碼ffmpeg -f concat -i mylist.txt -c copy output.mp4

下載ts合併
import requests
import time
import threading
import queue
import random

def write_ts(start_num, end_num):
    for i in range(start_num,end_num):
        a = random.randint(1,4)
        print(a)
        time.sleep(a)
        url = 'https://cagnagb/aaax%s.ts' % i
        response = requests.get(url, headers=headers)
        with open("./ts_files/%s.ts" % i, "wb") as f:
            f.write(response.content)

        print('%s 的 %s 完成' % (threading.current_thread().name,i))


def create_thread(total_num,thread_num):
    count_num = thread_num - 1
    int_num = int(total_num/count_num)
    mod_num = total_num%count_num
    every_num = (total_num - mod_num)/count_num

    for x in range(count_num):
        start_num = every_num*x
        end_num = every_num*(x + 1)
        num2 = x + 1
        threading.Thread(target=write_ts,args=(int(start_num), int(end_num)), name='t%s' % int(num2)).start()



    threading.Thread(target=write_ts,args=(int(every_num * count_num),int(every_num * count_num + mod_num)), name='t%s'%int(thread_num)).start()


def write_mylist(num,path1):
    arr1 = ["file '%s.ts'" % x for x in range(num)]
    with open(path1, "w") as file:
        file.write('\n'.join(arr1))

    print('mylist.txt寫入完成,請執行命令 【ffmpeg -f concat -i mylist.txt -c copy output.mp4】')


if __name__ == '__main__':

    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36'}

    total_num = 2308
    thread_num = 60
    threading.Lock()
    create_thread(total_num, thread_num)

    write_mylist_path = r"D:\pagcd\mylist.txt"
    write_mylist(total_num,write_mylist_path)



相關文章