Skip to main content
 首页 » 编程设计

python之在 GAE 上使用 Python 将 .ts 文件从 m3u8 合并到单个 mp4(无 ffmpeg)

2025年05月04日17yxwkf

我正在尝试在 GAE 上使用 python 下载文件列表并将其从 m3u8 list 合并到单个 mp4 视频文件。 现在我正在下载文件并按照它们在播放列表中的顺序将它们附加到同一个"new"文件。新文件可以播放,但只能与部分播放器一起播放。可能是因为大部分玩家搞不清楚文件类型——没有容器。 寻找一种将 mp4 容器元数据写入合并文件的方法。

代码是用 python 编写的,应该可以在 Google 应用引擎上运行。这就是我现在得到的(工作但没有容器标签)。

    gcs_file = cloudstorage.open(filename, 'w', content_type='video/mp4') 
    response = urlfetch.fetch(link) 
    base_link = link.rsplit('/', 1)[0] 
 
    if response.content: 
        lines = response.content.split('\n') 
        logging.info('merge_m3u8() - got response for m3u8: %s' % response.content) 
        ts_filenames = [line.rstrip() for line in lines if line.rstrip().endswith('.ts')] 
        logging.info('merge_m3u8() - found %d items in playlist' % len(ts_filenames)) 
        for ts_file in ts_filenames: 
            logging.info('merge_m3u8() - downloading file %s' % ts_file) 
            response = urlfetch.fetch('%s/%s' % (base_link, ts_file)) 
            gcs_file.write(response.content) 
 
        logging.info('merge_m3u8() - closing file %s' % filename) 
        gcs_file.close() 

请您参考如下方法:

有一个容器,这个容器就是TS。解析ts和写mp4不用ffmpeg也能搞定,但是代码量大。 (我用 C 编写了一个版本,专门设计为尽可能小,但它仍然超过 2000 SLOC)。格式非常不同,因此您不能简单地添加 mp4 元数据。你必须 demux 和 remux。但是,除了 ffmpeg 之外还有其他选项。例如 mp4box。