# 如何处理 TS 文件？

- Link: https://www.ixiqin.com/2017/09/06/how-to-deal-with-ts-file/
- Published: 2017-09-06T01:09:40+08:00
- Author: 白宦成

文章目录

 [Toggle](https://www.ixiqin.com/2017/09/06/how-to-deal-with-ts-file/#)

 * [下载 TS 文件](https://www.ixiqin.com/2017/09/06/how-to-deal-with-ts-file/#%e4%b8%8b%e8%bd%bd_ts_%e6%96%87%e4%bb%b6)
 * [合并 TS 文件](https://www.ixiqin.com/2017/09/06/how-to-deal-with-ts-file/#%e5%90%88%e5%b9%b6_ts_%e6%96%87%e4%bb%b6)
 * [将 TS 文件转码为 mp4 文件](https://www.ixiqin.com/2017/09/06/how-to-deal-with-ts-file/#%e5%b0%86_ts_%e6%96%87%e4%bb%b6%e8%bd%ac%e7%a0%81%e4%b8%ba_mp4_%e6%96%87%e4%bb%b6)
       * [延展阅读](https://www.ixiqin.com/2017/09/06/how-to-deal-with-ts-file/#%e5%bb%b6%e5%b1%95%e9%98%85%e8%af%bb)

TS 文件是现在在移动端用的比较多的视频格式。
 下午导师转发给我一个链接，希望我能把
链接中的视频放在会议的官网。自然没有拒绝 晚上回来审查了下元素才发现原来是 ts 文件，
以往没有处理 ts文件的经验，今天顺便记录下

## 下载 TS 文件

TS 文件的命名很有规律，一般都是 xxx-01.ts 到 xxx-99.ts 具体的数目取决于视频的大小。
你可以通过 Google Chrome 的开发者工具来看最终有多少个文件。
 然后，可以使用下面的
命令来一键下载

    ```bash
    wget -r http://www.example.com/filename-{1..99}.ts
    ```

这样我们就成功的下载了这个视频的所有分片。

> 部分视频网站可能会有一些检测的机制，可能会比较麻烦

## 合并 TS 文件

下载完成后，我们就要来合并 TS 文件了。使用下面的命令，就可以合并文件了

    ```bash
    echo 'filename-'{1..99}.ts | tr " " "\n" > tslist
    while read line; do cat $line >> filename-all.ts; done < tslist
    ```

通过上述的命令，我们可以把99个ts文件合并成为一个 filename-all.ts 文件，这个ts文件
就可以借助播放器来直接播放了。

## 将 TS 文件转码为 mp4 文件

这里为了方便浏览和兼容，我将 ts 文件转为 mp4 格式，代码如下

    ```bash
    ffmpeg -i source.ts -acodec copy -vcodec copy target.mp4
    ```

 

#### 延展阅读

https://gistpages.com/posts/download-streaming-video-ts-files
