1 安装ffmpeg
(ubuntu 16.04)
sudo add-apt-repository ppa:kazam-team/unstable-series
sudo apt-get update && sudo apt-get install kazam
sudo apt-repository ppa:kirillshkrogalev/ffmpeg-next
sudo apt-get update
sudo apt-get install ffmpeg
2 mp4视频转gif
ffmpeg -ss 2 -t 12 -i test.mp4 -s 649x320 -r 15 output1.gif
其中,-ss接起始秒数,-t接持续时长,-s接输出分辨率,-r接输出帧率
3 mp4文件合并
方法一:
ffmpeg -i 1.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 1.ts
ffmpeg -i 2.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 2.ts
ffmpeg -i "concat:1.ts|2.ts" -acodec copy -vcodec copy -absf aac_adtstoasc output.mp4
先把MP4转成ts再合并,其实也可以用以下方法,转化格式没太大必要:
方法二:
ffmpeg -f concat -safe 0 -i filenames.txt -c copy output.mp4
其中,filenames文件中写入要合并的视频名称,格式如:
file /home/Documents/001/cut1.v264
file /home/Documents/001/cut2.v264
file /home/Documents/001/cut3.v264
4 处理h265视频aac音频的文件
以剪切视频文件的操作为例:
ffmpeg -ss 2 -t 25 -i input.mp4 -strict -2 output.mp4
如果不加“-strict -2”就会报错:
[aac @ 0x8be6e0] The encoder ‘aac’ is experimental but experimental codecs are not enabled, add ‘-strict -2’ if you want to use it.
5 查看视频详细信息
使用ffprobe命令直接加文件名即可。也可以以json形式输出:
ffprobe -v quiet -print_format json -show_format -show_streams test.mp4
输出示例如下:
{
"streams": [
{
"index": 0,
"codec_name": "hevc",
"codec_long_name": "HEVC (High Efficiency Video Coding)",
"profile": "Main",
"codec_type": "video",
"codec_time_base": "1/15",
"codec_tag_string": "hev1",
"codec_tag": "0x31766568",
"width": 2560,
"height": 1440,
"coded_width": 2560,
"coded_height": 1440,
"has_b_frames": 0,
"sample_aspect_ratio": "0:1",
"display_aspect_ratio": "0:1",
"pix_fmt": "yuvj420p",
"level": 150,
"color_range": "pc",
"color_space": "bt709",
"color_transfer": "bt709",
"color_primaries": "bt709",
"refs": 1,
"r_frame_rate": "15/1",
"avg_frame_rate": "167680000/11178223",
"time_base": "1/90000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 100604007,
"duration": "1117.822300",
"bit_rate": "1747605",
"nb_frames": "16768",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0
},
"tags": {
"creation_time": "2021-03-25 13:19:00",
"language": "und",
"handler_name": "VideoHandler"
}
},
{
"index": 1,
"codec_name": "pcm_alaw",
"codec_long_name": "PCM A-law / G.711 A-law",
"codec_type": "audio",
"codec_time_base": "1/8000",
"codec_tag_string": "alaw",
"codec_tag": "0x77616c61",
"sample_fmt": "s16",
"sample_rate": "8000",
"channels": 1,
"bits_per_sample": 8,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/90000",
"start_pts": 5670,
"start_time": "0.063000",
"duration_ts": 100597396,
"duration": "1117.748844",
"bit_rate": "64004",
"nb_frames": "8733",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0
},
"tags": {
"creation_time": "2021-03-25 13:19:00",
"language": "und",
"handler_name": "AudioHandler"
}
}
],
"format": {
"filename": "test.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "1117.822000",
"size": "268435456",
"bit_rate": "1921132",
"probe_score": 100,
"tags": {
"major_brand": "isom",
"minor_version": "512",
"compatible_brands": "isomiso2mp41",
"creation_time": "2021-03-25 13:19:00"
}
}
}