python教程

m3u8视频TS合并Python脚本

我的站长站 2023-02-21 人阅读

用python继写的ts合并m3u8视频功能,抽离出来分享给大家使用。

合并的本质还是一下ffmpeg,请自行去官网下载放置脚本能找到的位置

python使用3.9版本,python 2已经不用再去支持了。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import os
 
def run_cmd(s_cmd):
    print(s_cmd)
    assert(0==os.system(s_cmd))
 
def takeFilePathName(filepath):
    return os.path.basename(filepath)
 
def autoMergeTsFileToMp4(inputDir, outDir):
    if not os.path.isdir(inputDir):
        return
    files = os.listdir(inputDir)
    tsFiles = []
    for file in files:
        filepath = os.path.join(inputDir, file)
        if os.path.isdir(filepath):
            autoMergeTsFileToMp4(filepath, outDir)
        else:
            tsFiles.append(filepath)
    if len(tsFiles)<=0:
        return
    tsDirName = os.path.basename(inputDir)
    tsFileListPath = '{0}.txt'.format(inputDir)
    if not os.path.isdir(outDir):
        os.makedirs(outDir)
    mergeFilePath = os.path.join(outDir,'{0}.mp4'.format(tsDirName))
    with open(tsFileListPath,'w') as f:
        tsFiles.sort(key=takeFilePathName)
        for filePath in tsFiles:
            f.write("file '{0}'\n".format(filePath))
    isTsFile = tsFiles[0].endswith('.ts')
    if not isTsFile:
        return
    binFfmpeg='ffmpeg'
    s_cmd = '{0} -f concat -safe 0'.format(binFfmpeg)
    s_cmd += ' -i {0} -c copy {1}'.format(tsFileListPath,mergeFilePath)
    run_cmd(s_cmd)
 
def main():
    videoRootDir = 'D:\\极客时间\\video-data'
    outTsDir = os.path.join(videoRootDir,'outx','ts')
    outMp4Dir = os.path.join(videoRootDir,'outx','mp4')
    autoMergeTsFileToMp4(outTsDir, outMp4Dir)
 
if __name__ == '__main__':
    main()


相关专题
m3u8
m3u8
2023-11-16 12

m3u8是一种常见的视频文件格式,它主要用于流媒体播放.使用m3u8文件需要专业的播放器,我的站长站为大家整理了许多支持m3u8格式的播放器.同时我们也整理了许多m3u8...

相关推荐
  • Python脚本
  • 监测腾讯云轻量服务器流量超标关机python脚本

    脚本介绍一款监测腾讯云轻量应用服务器流量包使用情况,并根据配置进行警告和关机的Python脚本。GitHub:https://github.com/XiaoXinYo/Tencent_Cloud_LightHouse_Server_Guardian脚本功能仅用于轻量级服务器1.自动检测流量包剩余,可设置使用比2.自动关...

    python教程 75 1年前
  • Python无需认证QQ扫码登录脚本

    无需认证QQ扫码登录脚本python脚本,盗用JD的QQ登录,也可以改成其他网址。无需自己注册腾讯开发者,无需自己有一套网址去申请应用Get_QQ返回QQ号,也可以获取到QQ头像、好友等其他信息,请勿用于非法行为import requestsimport timefrom PIL import Imagedef...

    python教程 291 2年前
  • 最新python织梦dedecms远程执行脚本

    织梦CMS是使用最多的CMS之 一,但是漏洞也非常多。分享一款python写的织梦远程文件包含漏洞。修复此漏洞方法,请见文章底部。织梦CMS漏洞代码#! /usr/bin/env python#coding=utf-8#Joseph(小续)import requestsimport sysimport redef main():try:url="...

    服务器配置 282 4年前
最新更新