M3U8-TS文件合并为MP4文件

M3U8文件是一个索引文件,里面包好N个TS的分片文件,组成一个视频文件。目前在直播和点播中应用非常广泛。我们下载一个M3U8视频文件,就是下载了N个TS分片文件,导致我们手机相册中多了很多碎片的小视频文件。如果是羞羞的视频,更加不好意思了。删除都要删除半天,更不用说想把M3U8文件拷贝出来,放到电脑上观看欣赏。
例如给一个M3U8例子:https://tv2.youkutv.cc/2020/04/14/MbqulRmS8sjQGJG9/playlist.m3u8,解析出来的索引文件如下:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:19
#EXTINF:13.960000,
out000.ts
#EXTINF:6.320000,
out001.ts
#EXTINF:10.280000,
out002.ts
#EXTINF:10.320000,
out003.ts
#EXTINF:9.960000,
out004.ts
#EXTINF:12.240000,
out005.ts
#EXTINF:8.680000,
out006.ts
#EXTINF:8.280000,
out007.ts
#EXTINF:10.240000,
out008.ts
#EXTINF:15.000000,
out009.ts
#EXTINF:5.000000,
out010.ts
#EXTINF:14.200000,
out011.ts
#EXTINF:7.920000,
out012.ts
#EXTINF:8.920000,
out013.ts
#EXTINF:10.960000,
out014.ts
#EXTINF:7.920000,
out015.ts
#EXTINF:10.000000,
out016.ts
#EXTINF:12.040000,
out017.ts
#EXTINF:9.280000,
out018.ts
#EXTINF:17.160000,
out019.ts
#EXTINF:8.080000,
out020.ts
#EXTINF:6.840000,
out021.ts
#EXTINF:8.040000,
out022.ts
#EXTINF:11.640000,
out023.ts
#EXTINF:10.000000,
out024.ts
#EXTINF:7.760000,
out025.ts
#EXTINF:9.040000,
out026.ts
#EXTINF:13.760000,
out027.ts
#EXTINF:7.120000,
out028.ts
#EXTINF:10.560000,
out029.ts
#EXTINF:11.640000,
out030.ts
#EXTINF:7.880000,
out031.ts
#EXTINF:11.880000,
out032.ts
#EXTINF:12.640000,
out033.ts
#EXTINF:6.920000,
out034.ts
#EXTINF:8.320000,
out035.ts
#EXTINF:10.720000,
out036.ts
#EXTINF:8.840000,
out037.ts
#EXTINF:10.320000,
out038.ts
#EXTINF:9.760000,
out039.ts
#EXTINF:10.320000,
out040.ts
#EXTINF:10.800000,
out041.ts
#EXTINF:13.200000,
out042.ts
#EXTINF:7.480000,
out043.ts
#EXTINF:8.560000,
out044.ts
#EXTINF:10.160000,
out045.ts
#EXTINF:10.160000,
out046.ts
#EXTINF:9.360000,
out047.ts
#EXTINF:11.960000,
out048.ts
#EXTINF:10.640000,
out049.ts
#EXTINF:11.360000,
out050.ts
#EXTINF:8.040000,
out051.ts
#EXTINF:7.640000,
out052.ts
#EXTINF:10.480000,
out053.ts
#EXTINF:10.400000,
out054.ts
#EXTINF:9.360000,
out055.ts
#EXTINF:15.720000,
out056.ts
#EXTINF:4.600000,
out057.ts
#EXTINF:9.640000,
out058.ts
#EXTINF:17.800000,
out059.ts
#EXTINF:6.040000,
out060.ts
#EXTINF:9.400000,
out061.ts
#EXTINF:10.000000,
out062.ts
#EXTINF:10.000000,
out063.ts
#EXTINF:10.000000,
out064.ts
#EXTINF:10.000000,
out065.ts
#EXTINF:9.000000,
out066.ts
#EXTINF:10.000000,
out067.ts
#EXTINF:10.000000,
out068.ts
#EXTINF:8.920000,
out069.ts
#EXTINF:15.920000,
out070.ts
#EXTINF:5.200000,
out071.ts
#EXTINF:8.240000,
out072.ts
#EXTINF:13.200000,
out073.ts
#EXTINF:14.400000,
out074.ts
#EXTINF:8.960000,
out075.ts
#EXTINF:5.880000,
out076.ts
#EXTINF:10.000000,
out077.ts
#EXTINF:10.000000,
out078.ts
#EXTINF:6.720000,
out079.ts
#EXTINF:16.720000,
out080.ts
#EXTINF:10.000000,
out081.ts
#EXTINF:10.000000,
out082.ts
#EXTINF:10.000000,
out083.ts
#EXTINF:9.640000,
out084.ts
#EXTINF:9.080000,
out085.ts
#EXTINF:7.000000,
out086.ts
#EXTINF:10.000000,
out087.ts
#EXTINF:17.320000,
out088.ts
#EXTINF:9.920000,
out089.ts
#EXTINF:8.000000,
out090.ts
#EXTINF:6.040000,
out091.ts
#EXTINF:7.560000,
out092.ts
#EXTINF:10.000000,
out093.ts
#EXTINF:8.800000,
out094.ts
#EXTINF:10.000000,
out095.ts
#EXTINF:10.000000,
out096.ts
#EXTINF:12.200000,
out097.ts
#EXTINF:10.000000,
out098.ts
#EXTINF:17.240000,
out099.ts
#EXTINF:10.000000,
out100.ts
#EXTINF:9.680000,
out101.ts
#EXTINF:10.000000,
out102.ts
#EXTINF:10.000000,
out103.ts
#EXTINF:10.000000,
out104.ts
#EXTINF:10.000000,
out105.ts
#EXTINF:10.000000,
out106.ts
#EXTINF:10.000000,
out107.ts
#EXTINF:10.000000,
out108.ts
#EXTINF:9.080000,
out109.ts
#EXTINF:10.000000,
out110.ts
#EXTINF:10.000000,
out111.ts
#EXTINF:10.000000,
out112.ts
#EXTINF:10.000000,
out113.ts
#EXTINF:10.000000,
out114.ts
#EXTINF:10.000000,
out115.ts
#EXTINF:10.000000,
out116.ts
#EXTINF:6.040000,
out117.ts
#EXTINF:9.000000,
out118.ts
#EXTINF:10.000000,
out119.ts
#EXTINF:10.000000,
out120.ts
#EXTINF:10.000000,
out121.ts
#EXTINF:10.000000,
out122.ts
#EXTINF:10.000000,
out123.ts
#EXTINF:10.000000,
out124.ts
#EXTINF:10.000000,
out125.ts
#EXTINF:10.000000,
out126.ts
#EXTINF:10.000000,
out127.ts
#EXTINF:10.000000,
out128.ts
#EXTINF:10.000000,
out129.ts
#EXTINF:10.000000,
out130.ts
#EXTINF:10.000000,
out131.ts
#EXTINF:9.760000,
out132.ts
#EXTINF:10.000000,
out133.ts
#EXTINF:10.000000,
out134.ts
#EXTINF:10.000000,
out135.ts
#EXTINF:10.000000,
out136.ts
#EXTINF:10.000000,
out137.ts
#EXTINF:8.880000,
out138.ts
#EXTINF:9.240000,
out139.ts
#EXTINF:11.840000,
out140.ts
#EXTINF:10.000000,
out141.ts
#EXTINF:10.000000,
out142.ts
#EXTINF:8.280000,
out143.ts
#EXTINF:10.000000,
out144.ts
#EXTINF:16.400000,
out145.ts
#EXTINF:3.960000,
out146.ts
#EXTINF:10.160000,
out147.ts
#EXTINF:8.360000,
out148.ts
#EXTINF:11.160000,
out149.ts
#EXTINF:12.440000,
out150.ts
#EXTINF:7.520000,
out151.ts
#EXTINF:12.600000,
out152.ts
#EXTINF:14.400000,
out153.ts
#EXTINF:4.800000,
out154.ts
#EXTINF:13.280000,
out155.ts
#EXTINF:10.000000,
out156.ts
#EXTINF:6.520000,
out157.ts
#EXTINF:9.160000,
out158.ts
#EXTINF:10.000000,
out159.ts
#EXTINF:7.920000,
out160.ts
#EXTINF:17.960000,
out161.ts
#EXTINF:8.000000,
out162.ts
#EXTINF:10.000000,
out163.ts
#EXTINF:10.000000,
out164.ts
#EXTINF:8.680000,
out165.ts
#EXTINF:8.920000,
out166.ts
#EXTINF:15.880000,
out167.ts
#EXTINF:3.360000,
out168.ts
#EXTINF:11.200000,
out169.ts
#EXTINF:8.160000,
out170.ts
#EXTINF:10.920000,
out171.ts
#EXTINF:8.280000,
out172.ts
#EXTINF:8.640000,
out173.ts
#EXTINF:14.400000,
out174.ts
#EXTINF:10.000000,
out175.ts
#EXTINF:8.960000,
out176.ts
#EXTINF:10.000000,
out177.ts
#EXTINF:13.560000,
out178.ts
#EXTINF:10.000000,
out179.ts
#EXTINF:10.000000,
out180.ts
#EXTINF:10.000000,
out181.ts
#EXTINF:10.000000,
out182.ts
#EXTINF:10.000000,
out183.ts
#EXTINF:10.000000,
out184.ts
#EXTINF:10.000000,
out185.ts
#EXTINF:10.000000,
out186.ts
#EXTINF:10.000000,
out187.ts
#EXTINF:10.000000,
out188.ts
#EXTINF:10.000000,
out189.ts
#EXTINF:10.000000,
out190.ts
#EXTINF:10.000000,
out191.ts
#EXTINF:10.000000,
out192.ts
#EXTINF:10.000000,
out193.ts
#EXTINF:10.000000,
out194.ts
#EXTINF:9.200000,
out195.ts
#EXTINF:10.000000,
out196.ts
#EXTINF:10.000000,
out197.ts
#EXTINF:10.000000,
out198.ts
#EXTINF:8.720000,
out199.ts
#EXTINF:10.000000,
out200.ts
#EXTINF:13.040000,
out201.ts
#EXTINF:6.360000,
out202.ts
#EXTINF:10.000000,
out203.ts
#EXTINF:15.200000,
out204.ts
#EXTINF:7.960000,
out205.ts
#EXTINF:8.120000,
out206.ts
#EXTINF:10.000000,
out207.ts
#EXTINF:9.600000,
out208.ts
#EXTINF:9.720000,
out209.ts
#EXTINF:10.000000,
out210.ts
#EXTINF:10.000000,
out211.ts
#EXTINF:10.000000,
out212.ts
#EXTINF:9.160000,
out213.ts
#EXTINF:10.000000,
out214.ts
#EXTINF:10.000000,
out215.ts
#EXTINF:8.040000,
out216.ts
#EXTINF:10.000000,
out217.ts
#EXTINF:10.000000,
out218.ts
#EXTINF:10.000000,
out219.ts
#EXTINF:10.000000,
out220.ts
#EXTINF:10.000000,
out221.ts
#EXTINF:10.000000,
out222.ts
#EXTINF:10.000000,
out223.ts
#EXTINF:10.000000,
out224.ts
#EXTINF:8.240000,
out225.ts
#EXTINF:15.200000,
out226.ts
#EXTINF:6.200000,
out227.ts
#EXTINF:9.560000,
out228.ts
#EXTINF:10.000000,
out229.ts
#EXTINF:10.000000,
out230.ts
#EXTINF:10.000000,
out231.ts
#EXTINF:9.560000,
out232.ts
#EXTINF:9.640000,
out233.ts
#EXTINF:17.880000,
out234.ts
#EXTINF:8.600000,
out235.ts
#EXTINF:9.560000,
out236.ts
#EXTINF:10.000000,
out237.ts
#EXTINF:10.000000,
out238.ts
#EXTINF:8.000000,
out239.ts
#EXTINF:10.000000,
out240.ts
#EXTINF:10.000000,
out241.ts
#EXTINF:10.000000,
out242.ts
#EXTINF:10.000000,
out243.ts
#EXTINF:10.000000,
out244.ts
#EXTINF:10.000000,
out245.ts
#EXTINF:9.440000,
out246.ts
#EXTINF:14.240000,
out247.ts
#EXTINF:5.840000,
out248.ts
#EXTINF:6.600000,
out249.ts
#EXTINF:9.920000,
out250.ts
#EXTINF:11.080000,
out251.ts
#EXTINF:14.600000,
out252.ts
#EXTINF:7.160000,
out253.ts
#EXTINF:8.840000,
out254.ts
#EXTINF:11.320000,
out255.ts
#EXTINF:8.720000,
out256.ts
#EXTINF:8.240000,
out257.ts
#EXTINF:9.880000,
out258.ts
#EXTINF:18.480000,
out259.ts
#EXTINF:10.000000,
out260.ts
#EXTINF:8.760000,
out261.ts
#EXTINF:9.240000,
out262.ts
#EXTINF:6.120000,
out263.ts
#EXTINF:13.480000,
out264.ts
#EXTINF:10.000000,
out265.ts
#EXTINF:8.360000,
out266.ts
#EXTINF:6.080000,
out267.ts
#EXTINF:14.080000,
out268.ts
#EXTINF:7.520000,
out269.ts
#EXTINF:9.240000,
out270.ts
#EXTINF:10.000000,
out271.ts
#EXTINF:6.960000,
out272.ts
#EXT-X-ENDLIST

里面有273个分片文件,可以使用:https://github.com/JeffMony/M3U8ParserTools/blob/master/m3u8_function/parse_m3u8_info.py脚本获取当前的M3U8索引文件。
下载好视频文件如下:

PD1824:/sdcard/Android/data/com.jeffmony.videodemo/files/Video/Download/a03663b3bd0a2fe6fcb8bb36b657cf80 $ lslocal.m3u8   video_124.ts video_152.ts video_180.ts video_208.ts video_236.ts video_264.ts video_47.ts video_75.tsremote.m3u8  video_125.ts video_153.ts video_181.ts video_209.ts video_237.ts video_265.ts video_48.ts video_76.tsvideo_0.ts   video_126.ts video_154.ts video_182.ts video_21.ts  video_238.ts video_266.ts video_49.ts video_77.tsvideo_1.ts   video_127.ts video_155.ts video_183.ts video_210.ts video_239.ts video_267.ts video_5.ts  video_78.tsvideo_10.ts  video_128.ts video_156.ts video_184.ts video_211.ts video_24.ts  video_268.ts video_50.ts video_79.tsvideo_100.ts video_129.ts video_157.ts video_185.ts video_212.ts video_240.ts video_269.ts video_51.ts video_8.tsvideo_101.ts video_13.ts  video_158.ts video_186.ts video_213.ts video_241.ts video_27.ts  video_52.ts video_80.tsvideo_102.ts video_130.ts video_159.ts video_187.ts video_214.ts video_242.ts video_270.ts video_53.ts video_81.tsvideo_103.ts video_131.ts video_16.ts  video_188.ts video_215.ts video_243.ts video_271.ts video_54.ts video_82.tsvideo_104.ts video_132.ts video_160.ts video_189.ts video_216.ts video_244.ts video_272.ts video_55.ts video_83.tsvideo_105.ts video_133.ts video_161.ts video_19.ts  video_217.ts video_245.ts video_28.ts  video_56.ts video_84.tsvideo_106.ts video_134.ts video_162.ts video_190.ts video_218.ts video_246.ts video_29.ts  video_57.ts video_85.tsvideo_107.ts video_135.ts video_163.ts video_191.ts video_219.ts video_247.ts video_3.ts   video_58.ts video_86.tsvideo_108.ts video_136.ts video_164.ts video_192.ts video_22.ts  video_248.ts video_30.ts  video_59.ts video_87.tsvideo_109.ts video_137.ts video_165.ts video_193.ts video_220.ts video_249.ts video_31.ts  video_6.ts  video_88.tsvideo_11.ts  video_138.ts video_166.ts video_194.ts video_221.ts video_25.ts  video_32.ts  video_60.ts video_89.tsvideo_110.ts video_139.ts video_167.ts video_195.ts video_222.ts video_250.ts video_33.ts  video_61.ts video_9.tsvideo_111.ts video_14.ts  video_168.ts video_196.ts video_223.ts video_251.ts video_34.ts  video_62.ts video_90.tsvideo_112.ts video_140.ts video_169.ts video_197.ts video_224.ts video_252.ts video_35.ts  video_63.ts video_91.tsvideo_113.ts video_141.ts video_17.ts  video_198.ts video_225.ts video_253.ts video_36.ts  video_64.ts video_92.tsvideo_114.ts video_142.ts video_170.ts video_199.ts video_226.ts video_254.ts video_37.ts  video_65.ts video_93.tsvideo_115.ts video_143.ts video_171.ts video_2.ts   video_227.ts video_255.ts video_38.ts  video_66.ts video_94.tsvideo_116.ts video_144.ts video_172.ts video_20.ts  video_228.ts video_256.ts video_39.ts  video_67.ts video_95.tsvideo_117.ts video_145.ts video_173.ts video_200.ts video_229.ts video_257.ts video_4.ts   video_68.ts video_96.tsvideo_118.ts video_146.ts video_174.ts video_201.ts video_23.ts  video_258.ts video_40.ts  video_69.ts video_97.tsvideo_119.ts video_147.ts video_175.ts video_202.ts video_230.ts video_259.ts video_41.ts  video_7.ts  video_98.tsvideo_12.ts  video_148.ts video_176.ts video_203.ts video_231.ts video_26.ts  video_42.ts  video_70.ts video_99.tsvideo_120.ts video_149.ts video_177.ts video_204.ts video_232.ts video_260.ts video_43.ts  video_71.tsvideo_121.ts video_15.ts  video_178.ts video_205.ts video_233.ts video_261.ts video_44.ts  video_72.tsvideo_122.ts video_150.ts video_179.ts video_206.ts video_234.ts video_262.ts video_45.ts  video_73.tsvideo_123.ts video_151.ts video_18.ts  video_207.ts video_235.ts video_263.ts video_46.ts  video_74.ts

如果能将这些TS文件合成一个视频文件就好了。

TS文件合成一个MP4视频,需要的注意点有:

  • 有些M3U8视频是加密的,TS源文件需要解密才能播放

  • TS文件一个个拼接的方式最后得到的还是一个TS视频,只不过比较大一点,并不是后缀名改成.mp4就是MP4视频了。

加密的视频

M3U8中的EXT-X-KEY中就包含M3U8的加密方式以及密钥。
例如http://video.yjf138.com:8091/20180812/6yl0Q2YZ/index.m3u8中就有
#EXT-X-KEY:METHOD=AES-128,URI='key.key'
可以看出加密方式是AES-128对称加密,密钥是key.key
转化为链接就是:https://video.yjf138.com:8091/20180812/6yl0Q2YZ/1500kb/hls/key.key
那我们就可以使用AES-128解密了,通常的做法是:

/**
* 解密ts
*
* @param sSrc ts文件字节数组
* @param sKey 密钥
* @return 解密后的字节数组
*/
private static byte[] decrypt(byte[] sSrc, String sKey, String method) {
try {
if (StringUtils.isNotEmpty(method) && !method.contains('AES')) {
throw new M3u8Exception('未知的算法!');
}
// 判断Key是否正确
if (StringUtils.isEmpty(sKey)) {
return sSrc;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print('Key长度不是16位');
return null;
}
Cipher cipher = Cipher.getInstance('AES/CBC/PKCS7Padding');
SecretKeySpec keySpec = new SecretKeySpec(sKey.getBytes('utf-8'), 'AES');
// 如果m3u8有IV标签,那么IvParameterSpec构造函数就把IV标签后的内容转成字节数组传进去
AlgorithmParameterSpec paramSpec = new IvParameterSpec(new byte[16]);
cipher.init(Cipher.DECRYPT_MODE, keySpec, paramSpec);
return cipher.doFinal(sSrc);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

但是在Android平台上,这样的写法并不能兼容所有的场景,因为不同Android level上面的crypto写法不同,调用系统的api得到的结果也会不一样,所以最好将解密场景放在native层,使用openssl库来帮我们实现解密。

TS转化为MP4

之前说过,TS合并通常的做法使用InputStream读取一个一个的TS分片,然后利用OutputStream写入本地的MP4文件中,这样看上去好像是生成了一个新的MP4文件,但是实际上这个新的视频是真正的MP4格式吗?

显然不是,因为MP4的封装格式和TS是完全不一样的。mpegts一般是放在m3u8索引文件中的分片,单独拿出来用虽然可以播放,但是有很多问题:(1)无法有效seek;(2)获取不了总时长;
mp4视频封装格式是目前使用最为广泛的视频格式,一方面因为mp4没有什么版权的问题,另一方面是兼容性非常好,Android平台原生支持,MediaExtractor、MediaMuxer等原生的api接口都能很好地支持mp4封装格式。

下面是同一个视频为mpegts封装格式和mp4封装格式时的具体信息:

Input #0, mpegts, from 'merge_video.ts':  Duration: 00:45:28.63, start: 1.454944, bitrate: 1047 kb/s  Program 1    Metadata:      service_name    : Service01      service_provider: FFmpeg    Stream #0:0[0x100](und): Audio: mp3 ([3][0][0][0] / 0x0003), 44100 Hz, stereo, fltp, 128 kb/s    Stream #0:1[0x101]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1280x720 [SAR 53113:53120 DAR 53113:29880], 25 fps, 25 tbr, 90k tbn, 50 tbc
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:45:28.65, start: 1.453991, bitrate: 956 kb/s
Stream #0:0(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream #0:1(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 53113:53120 DAR 53113:29880], 820 kb/s, SAR 15175:15177 DAR 242800:136593, 25 fps, 25 tbr, 90k tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler

mp4视频多了一些头部,这些头部,被称为moov信息。

如何将mpegts格式的视频转化为mp4的视频?

最好的做法就是将最终生成的文件按照MP4的封装规则重写一遍,这样最终生成的文件肯定是MP4的文件。

我们不用对照MP4的位flag来一个个生成,只要借助ffmpeg来帮我们实现这个转化就可以了。

  • 对源文件进行解封装处理,取出源文件的音频流和视频流

  • 创建目标文件的封装格式头信息。

  • 读取源文件音频流和视频流中的包数据、帧数据,然后按照规则封装到目标文件格式中。

1.打开源文件

int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);
识别源文件的封装格式。

2.取出源文件的轨道流信息

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
这个函数操作之后,AVFormatContext结构体中:

  • unsigned int nb_streams; 填充轨道数

  • AVStream **streams; 填充轨道index对应的轨道信息

3.为输出文件创建AVFormatContext

int avformat_alloc_output_context2(AVFormatContext **ctx, ff_const59 AVOutputFormat *oformat,
const char *format_name, const char *filename);
操作之后,输出文件的AVFormatContext初始化。

4.设置输出文件对应流的编码参数

    for (i = 0; i < ifmt_ctx->nb_streams; i++) {        AVStream *out_stream;        AVStream *in_stream = ifmt_ctx->streams[i];        AVCodecParameters *in_codecpar = in_stream->codecpar;

        if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&            in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&            in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {            stream_mapping[i] = -1;            continue;        }

        stream_mapping[i] = stream_index++;

        out_stream = avformat_new_stream(ofmt_ctx, NULL);        if (!out_stream) {            fprintf(stderr, 'Failed allocating output stream\n');            ret = AVERROR_UNKNOWN;            goto end;        }

        ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);        if (ret < 0) {            fprintf(stderr, 'Failed to copy codec parameters\n');            goto end;        }        out_stream->codecpar->codec_tag = 0;    }

这儿的操作主要是将输入文件各路流中对应的codecpar取出来,然后赋给输出流。
这儿有个概念区分下,封装格式和编码格式有很大的不同。这儿我们只希望改变封装格式,并不要改变编码格式,所以codecpar直接复制过来就行了。

5.写输出文件的封装头部

int avformat_write_header(AVFormatContext *s, AVDictionary **options);
最终会根据你输出的封装格式写入相应的header信息,例如输出的封装格式是mp4,那么最终会调用movenc.c中的write_header函数写入header:

AVOutputFormat ff_mp4_muxer = {
.name = 'mp4',
.long_name = NULL_IF_CONFIG_SMALL('MP4 (MPEG-4 Part 14)'),
.mime_type = 'video/mp4',
.extensions = 'mp4',
.priv_data_size = sizeof(MOVMuxContext),
.audio_codec = AV_CODEC_ID_AAC,
.video_codec = CONFIG_LIBX264_ENCODER ?
AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
.init = mov_init,
.write_header = mov_write_header,
.write_packet = mov_write_packet,
.write_trailer = mov_write_trailer,
.deinit = mov_free,
.flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
.codec_tag = (const AVCodecTag* const []){ codec_mp4_tags, 0 },
.check_bitstream = mov_check_bitstream,
.priv_class = &mp4_muxer_class,
};

6.读取AVPacket

int av_read_frame(AVFormatContext *s, AVPacket *pkt);
需要对其AVPacket中如下参数:

  • pkt.stream_index :轨道索引

  • pkt.pts :显示时间戳

  • pkt.dts :解码时间戳

  • pkt.duration :市场

7.AVPacket写入输出文件

int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
将处理过的AVPacket数据写入输出文件。

8.写封装格式文件尾

int av_write_trailer(AVFormatContext *s);

编译这个文件:
gcc -g -o remuxing remuxing.c `pkg-config --libs --cflags libavutil libavformat`

生成的remuxing可执行文件,执行如下:
./remuxing merge_video.ts output.mp4

本文具体的源码见:https://github.com/JeffMony/AVInterview/blob/master/ffmpeg_example/remuxing.c

Android平台接入修改格式功能

上面已经清晰地分析了完整的代码流程,如果我们要接入Android平台也很简单。但是直接将代码复制到Android平台,编译运行首先会发生如下错误:

2020-09-25 00:36:53.585 25265-25414/com.android.ffmpegdemo E/FFmpeg_JeffMony: dimensions not set2020-09-25 00:36:53.585 25265-25414/com.android.ffmpegdemo E/FFmpegDemo: Error occurred when opening output file, ret=-222020-09-25 00:36:53.585 25265-25414/com.android.ffmpegdemo E/FFmpegDemo: Error occurred: Invalid argument

查看代码,发现是运行到ret = avformat_write_header(ofmt_ctx, NULL);出现的问题。

提示了dimensions not set
追踪一下代码,发现在mux.c中的init_muxer函数中有如下代码:

case AVMEDIA_TYPE_VIDEO:
if ((par->width <= 0 || par->height <= 0) &&
!(of->flags & AVFMT_NODIMENSIONS)) {
av_log(s, AV_LOG_ERROR, 'dimensions not set\n');
ret = AVERROR(EINVAL);
goto fail;
}

这就比较明确地告知当前设置的AVFormatContext中的视频流width、height没有设置。

你需要设置一下,怎么设置?

        if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {            out_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;            out_stream->codecpar->width = in_codecpar->width;            out_stream->codecpar->height = in_codecpar->height;        }

最终在Android平台上就可以实现修改视频封装格式的目的了。

最终具体的代码见项目:
https://github.com/JeffMony/VideoDownloader

(0)

相关推荐

  • ffprobe输入与输出信息详解

    ffprobe是ffmpeg提供的三大工具之一,用来查看音视频文件的各种信息,比如:封装格式.音频/视频流信息.数据包信息等. ffprobe的源码是ffprobe.c,开发过程中如果想获取ffpro ...

  • 微博离线缓存的视频在哪里?

    首先打开文件管理, 文件管理 再选择内部储存, 我的手机 再上滑找到Android, Android 再找到data, data 再上滑找到com.sina.video, com.sina.video ...

  • 微信视频号视频怎么下载,视频号视频怎么保存到手机

    微信视频号视频怎么下载,视频号视频怎么保存到手机 视频号有多火,不用咱们多说,了解下网上层出不尽的各种培训课程就知道了. 如果你也经常关注视频号,肯定也已经发现,视频号除了点赞.评论.收藏.转发外,并 ...

  • 如何将视频格式转化为音频格式

    首先打开百度搜索Movavi Video Converter 关键词,我们找到对应的下载网站点击进入下载 下载完成后,点击安装运行.成功后,打开Movavi Video Converter 在这里拖入 ...

  • 文件操作:mp4文件怎么转换成mp3格式?

    今天给大家分享一下我们日常生活中经常会遇到的一些文件操作,MP4视频文件与MP3音频文件.MP4是互联网上最流行的视频格式,我们从一些资源网上面获取到的视频文件大部分都是以MP4格式存储的.而MP3是 ...

  • 如何把多个Excel文件合并到一个文件中?

    如何把多个Excel文件合并到一个文件中?

  • TS文件合并命令

    可以使用以下DOS命令达到目的(即"开始"菜单,"运行",输入 cmd 再按回车): (此处假设你要合并的高清文件位于 E:\temps 这个位置) copy  ...

  • Revit两个文件合并成一个怎么做?关于Revit如何合并参考文件

    来源丨益埃毕教育 在我们进行创建项目时,难免需要借鉴其他人或已经做好的参考文件,那么如何将参考文件与自己创建的项目合并呢,下面分享给大家. 1.新建项目或打开现有项目,文件中已存在现有标高轴网 2.点 ...

  • 怎么把100多个EXCEL文件合并成一个

    2020-07-10·策划专员 可以通过更改excel代码来合并多个文件. 详细步骤: 1.新建一个文件夹. 2.将要合并的表格放到里面. 3.新建一个表格. 4.用excel打开. 5.右击Shee ...

  • C# 将多个图片合并成TIFF文件的两种方法

    dotNET跨平台 今天 以下文章来源于WPF UI ,作者Gxy WPF UIWPF UI 设计,WPF教程,MVVM,C#程序设计~ 最近需要用到TIF格式的文件,研究了一段时间,终于有点结果了, ...

  • 华为电脑上怎么把两个pdf合并成一个文件?

    最近新购入一部华为电脑,还处于新鲜期,所以我需要进行什么操作能在电脑上进行的我都会选择在电脑上操作!哈哈!这不昨天需要整理一些pdf格式的文件,要将两个pdf格式的文件给合并成一个的时候,小编自然也是 ...

  • rmvb视频文件怎么转换mp4格式?

    小编因为工作的原因,在日常生活工作中是经常需要接触到各种格式类型的,有些格式是我们经常见到地视频类型,而有一些我们就很少见到了.像rmvb这种格式我们在日常生活其实是不常见的,因为由于rmvb格式的视 ...

  • 视频格式处理:腾讯视频格式怎么转换成mp4文件?

    网络上的各大视频播放平台是基本上都有自己独有的视频格式的,而这种独有格式的视频不出意外是都有各自的权限,也就是说除了它所指定的播放平台,其它平台都是不能直接播放的除非转换成mp4的视频通用格式!而腾讯 ...