M3U8-TS文件合并为MP4文件

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

例如给一个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 $ ls
local.m3u8 video_124.ts video_152.ts video_180.ts video_208.ts video_236.ts video_264.ts video_47.ts video_75.ts
remote.m3u8 video_125.ts video_153.ts video_181.ts video_209.ts video_237.ts video_265.ts video_48.ts video_76.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_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.ts
video_120.ts video_149.ts video_177.ts video_204.ts video_232.ts video_260.ts video_43.ts video_71.ts
video_121.ts video_15.ts video_178.ts video_205.ts video_233.ts video_261.ts video_44.ts video_72.ts
video_122.ts video_150.ts video_179.ts video_206.ts video_234.ts video_262.ts video_45.ts video_73.ts
video_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 streamn");
ret = AVERROR_UNKNOWN;
goto end;
}

ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
if (ret < 0) {
fprintf(stderr, "Failed to copy codec parametersn");
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 set
2020-09-25 00:36:53.585 25265-25414/com.android.ffmpegdemo E/FFmpegDemo: Error occurred when opening output file, ret=-22
2020-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 setn");
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

图片

作者:小安安开火车 | 来源:音视频平凡之路

版权声明:本文内容转自互联网,本文观点仅代表作者本人。本站仅提供信息存储空间服务,所有权归原作者所有。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至1393616908@qq.com 举报,一经查实,本站将立刻删除。

(0)

相关推荐

发表回复

登录后才能评论