前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python合并ts视频

python合并ts视频

作者头像
py3study
发布2020-01-08 14:23:41
3.2K0
发布2020-01-08 14:23:41
举报
文章被收录于专栏:python3python3

合并ts视频

代码语言:javascript
复制
#!/usr/bin/env/python
#_*_coding:utf-8_*_
#Data:2017-10-02
#Auther:苏莫
#Link:http://blog.csdn.net/lingluofengzang
#PythonVersion:python2.7
#filename:convert_m3u8.py

import os
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

# 检查文件路径及文件是否正确
def check_path_file(_path, _file):

    # 判断路径是否存在
    # os.path.isdir(path)  判断路径是否为目录
    # os.path.isabs(path)  判断是否为绝对路径
    if os.path.isdir(_path) or os.path.isabs(_path):
        # 判断文件是否存在
        # os.path.join(path1[, path2[, ...]])  把目录和文件名合成一个路径
        # os.path.exists(path)  路径存在则返回True,路径损坏返回False
        if os.path.exists(os.path.join(_path, _file)):
            print u'>>>[-] 目标文件已经存在。'
            exit(0)

        return True

    else:
        print u'>>>[-] 路径不存在。'
        exit(0)

# 更改后缀名为[.ts]
def change_file_name(_path):

    # 获取路径下的文件名
    files = os.listdir(_path)

    for filename in files:
        # 文件名分割 文件名称+后缀
        portion = os.path.splitext(filename)
        if portion[1] != '.m3u8' or portion[1] == '':
            newname = portion[0]+".ts"
            # 切换到文件所在路径
            os.chdir(_path)
            # 更换文件后缀
            os.rename(filename,newname)

    return True

# 对[.ts]文件进行排序
def sort_file(_path, num = '1'):

    if num == '1':
        change_file_name(_path)
    file_lists = os.listdir(_path)

    file_list = []

    for file in file_lists:
        portion = os.path.splitext(file)
        if portion[1] == '.ts':
            file_list.append(int(portion[0]))

    file_list.sort()
    return file_list

# 合并文件
def convert_file(_path, files, filename):

    tmp = []
    for file in files:
        tmp.append(str(file) + '.ts')
    # 合并ts文件
    os.chdir(_path)
    shell_str = '+'.join(tmp)
    shell_str = 'copy /b '+ shell_str + ' ' + filename
    os.system(shell_str)
    # 删除ts和m3u8文件
    os.system('del /Q *.ts')
    os.system('del /Q *.m3u8')

if __name__ == '__main__':

    print '-' * 60 + '\n'
    print u'将m3u8格式的视频转换成mp4格式'.center(60) + '\n'
    print '-' * 60

    try:

        _path = raw_input(unicode('>>>[+] 请输入m3u8视频所在目录\n>>>[+] ').encode('gbk'))
        _file = raw_input(unicode('>>>[+] 请输入mp4的文件名\n>>>[+] ').encode('gbk'))+'.mp4'
        print u'>>>[+] 是否需要将m3u8视频后缀名转换为[.ts]'
        num = raw_input('>>>[+] Yes:1 No:2\n>>>[+] [1]')

        flag = check_path_file(_path, _file)

        if flag:
            if num == '2':
                files = sort_file(_path, num)
            else:
                files = sort_file(_path)
            print '-' * 60
            convert_file(_path, files, _file)

    except Exception as e:
        print e

合并后视频出现卡顿、加速播放等问题,解决方法不知,如果有知道怎么解决的朋友,欢迎联系,不胜感激。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档