FFMPEG视频切片TS文件添加PNG文件头伪图片上传

FFMPEG视频切片TS文件添加PNG文件头伪图片上传

韩小韩
2022-08-24 / 8 评论 / 2,091 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年08月25日,已超过612天没有更新,若内容或图片失效,请留言反馈。

前期准备

FFMPEG视频切片TS文件添加PNG文件头伪图片上传

整体步奏

  1. 对MP4视频文件转换单TS文件
  2. 对单TS文件切割成多个TS切片文件并生成M3U8文件
  3. 对TS文件进行 "偷天换日 改头换面" 伪图片处理
  4. 上传任意图床取 "PNG" 文件地址

最后预览视频

开始 Start

FFMPEG视频切片TS文件添加PNG文件头伪图片上传

代码片段

# Mp4 文件名字 不需要加 .mp4 后缀
vName = 'SaiBoPengKe'
cmd_str = f'ffmpeg -y -i {vName}.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb {vName}.ts'
subprocess.run(cmd_str, encoding="utf-8", shell=True)
print(f'标准 Mp4 转换到 TS 成功!')

filePath = f'./{vName}'
if os.path.exists(filePath) == False:
    os.makedirs(filePath)
cmd_str = f'ffmpeg -i {vName}.ts -c copy -map 0 -f segment -segment_list ./{vName}/index.m3u8 -segment_time 2 ./{vName}/output%03d.ts'
subprocess.run(cmd_str, encoding="utf-8", shell=True)
print(f'TS 切片 成功!')

file_list = os.listdir(filePath)
for i in file_list:
    if i.endswith(".ts"):
        new_name = i.replace(".ts", ".png")
        os.rename(f'{filePath}/' + i, f'{filePath}/' + new_name)
print("TS重命名为PNG 成功!")

file_list = os.listdir(filePath)
rewritePath = f'{filePath}Png/'
if os.path.exists(rewritePath) == False:
    os.makedirs(rewritePath)
for i in file_list:
    if i.endswith(".png"):
        copyfile("PNG", f'{rewritePath}/' + i)
    else:
        copyfile(f'{filePath}/' + i, f'{rewritePath}/' + i)
file_list = os.listdir(rewritePath)
for i in file_list:
    if i.endswith(".png"):
        bin_file = open(f'{filePath}/' + i, 'rb')  # 打开二进制文件
        # 合并文件
        with open(f'{rewritePath}/' + i, 'ab') as f:
            f.write(bin_file.read())
            bin_file.close()
print("PNG元数据转PNG-TS 成功!")
os.remove(f'{vName}.ts')
shutil.rmtree(filePath)
print("临时文件删除成功")

FFMPEG视频切片TS文件添加PNG文件头伪图片上传

89504E470D0A1A0A0000000D494844520000007C0000007508060000008C118576000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000011049444154785EEDD10101000008C320FB97BE411815B8855238A6704CE198C23185630AC7148E29

代码整合脚本

# utf-8
import subprocess
import os
import shutil
from shutil import copyfile

# Mp4 文件名字
vName = 'sbpk'
# 标准mp4转TS格式------------------------------------------------------------------------------
cmd_str = f'ffmpeg -y -i {vName}.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb {vName}.ts'
subprocess.run(cmd_str, encoding="utf-8", shell=True)
print(f'标准 Mp4 转换到 TS 成功!')


# TS切片------------------------------------------------------------------------------
filePath = f'./{vName}'
if os.path.exists(filePath) == False:
    os.makedirs(filePath)
cmd_str = f'ffmpeg -i {vName}.ts -c copy -map 0 -f segment -segment_list ./{vName}/index.m3u8 -segment_time 2 ./{vName}/output%03d.ts'
subprocess.run(cmd_str, encoding="utf-8", shell=True)
print(f'TS 切片 成功!')


# TS重命名为PNG------------------------------------------------------------------------------
file_list = os.listdir(filePath)
for i in file_list:
    if i.endswith(".ts"):
        new_name = i.replace(".ts", ".png")
        os.rename(f'{filePath}/' + i, f'{filePath}/' + new_name)
print("TS重命名为PNG 成功!")


# PNG文件添加PNG文件头------------------------------------------------------------------------------
file_list = os.listdir(filePath)
rewritePath = f'{filePath}Png/'
if os.path.exists(rewritePath) == False:
    os.makedirs(rewritePath)
for i in file_list:
    if i.endswith(".png"):
        copyfile("PNG", f'{rewritePath}/' + i)
    else:
        copyfile(f'{filePath}/' + i, f'{rewritePath}/' + i)
file_list = os.listdir(rewritePath)
for i in file_list:
    if i.endswith(".png"):
        bin_file = open(f'{filePath}/' + i, 'rb')  # 打开二进制文件
        # 合并文件
        with open(f'{rewritePath}/' + i, 'ab') as f:
            f.write(bin_file.read())
            bin_file.close()
print("PNG元数据转PNG-TS 成功!")
os.remove(f'{vName}.ts')
shutil.rmtree(filePath)
print("临时文件删除成功")

2

评论 (8)

取消
  1. 头像
    yangfugui
    Windows 10 Windows 10 / Google Chrome Google Chrome 中国四川省成都市电信

    我转换成.png文件之后,u3m8文件里面还记录的.ts文件名称,我要怎么处理才能正常播放呢,能不能给我详细说一下啊

    回复
  2. 头像
    Kaka
    iOS 16.5 iOS 16.5 / Microsoft Edge Microsoft Edge 中国湖北省武汉市电信

    mpvplayer播放失败
    Vlc播放失败
    应该是要改ffmpeg

    回复
  3. 头像
    1874
    Windows 10 Windows 10 / Microsoft Edge Microsoft Edge 中国江西省宜春市电信

    哥切完了怎么操作

    回复
    1. 头像
      韩小韩 作者
      Windows 10 Windows 10 / Google Chrome Google Chrome 中国江苏省南京市电信
      @ 1874

      找个图床狠狠上传!!!!

      回复
  4. 头像
    666
    iOS 15.7 iOS 15.7 / Google Chrome for iOS Google Chrome for iOS 中国北京市联通

    但是播放的时候就出问了,ffmpeg 怎么修改去掉头部的伪装字节呢

    回复
    1. 头像
      韩小韩 作者
      Windows 10 Windows 10 / Google Chrome Google Chrome 中国江苏省南京市电信
      @ 666

      播放的时候没问题啊,请您描述清晰

      回复
  5. 头像
    哥斯拉
    Windows 10 Windows 10 / Google Chrome Google Chrome 美国3COM公司企业网

    高手,这是个好方法
    以前用阿里图床也能这样搞,后来就不行了。

    回复
    1. 头像
      韩小韩 作者
      Windows 10 Windows 10 / Google Chrome Google Chrome 中国江苏省南京市电信
      @ 哥斯拉

      大厂的安全措施比较到位,满大街的图床都可以试试表情

      回复