音视频处理功能

音视频meta信息

获取指定音频、视频资源的元信息,例如:宽、高、码率、帧率、文件大小、时长

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{
"streams": [
{
"index": 0,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "LC",
"codec_type": "audio",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "44100",
"channels": 1,
"channel_layout": "mono",
"bits_per_sample": 0,
"id": "0x1",
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/44100",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 2284491,
"duration": "51.802517",
"bit_rate": "128039",
"nb_frames": "2231",
"extradata_size": 2,
"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,
"timed_thumbnails": 0,
"captions": 0,
"descriptions": 0,
"metadata": 0,
"dependent": 0,
"still_image": 0
},
"tags": {
"creation_time": "2024-02-23T13:46:34.000000Z",
"language": "eng",
"handler_name": "SoundHandle",
"vendor_id": "[0][0][0][0]"
}
},
{
"index": 1,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "Constrained Baseline",
"codec_type": "video",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 1080,
"height": 1920,
"coded_width": 1080,
"coded_height": 1920,
"closed_captions": 0,
"film_grain": 0,
"has_b_frames": 0,
"pix_fmt": "yuv420p",
"level": 40,
"color_range": "tv",
"color_space": "bt709",
"color_transfer": "bt709",
"color_primaries": "bt709",
"chroma_location": "left",
"field_order": "progressive",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"id": "0x2",
"r_frame_rate": "120/1",
"avg_frame_rate": "27405000/2324909",
"time_base": "1/90000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 4649818,
"duration": "51.664644",
"bit_rate": "6153719",
"bits_per_raw_sample": "8",
"nb_frames": "609",
"extradata_size": 33,
"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,
"timed_thumbnails": 0,
"captions": 0,
"descriptions": 0,
"metadata": 0,
"dependent": 0,
"still_image": 0
},
"tags": {
"creation_time": "2024-02-23T13:46:34.000000Z",
"language": "eng",
"handler_name": "VideoHandle",
"vendor_id": "[0][0][0][0]"
}
}
],
"format": {
"filename": "1gO0elRuVp8xPw2wvIP3VwZx3Zo.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": "51.802500",
"size": "40591710",
"bit_rate": "6268687",
"probe_score": 100,
"tags": {
"major_brand": "mp42",
"minor_version": "0",
"compatible_brands": "isommp42",
"creation_time": "2024-02-23T13:46:34.000000Z",
"com.android.version": "12"
}
}
}

生成上面的meta信息,底层使用的算法是ffprobe,命令行如下:

1
ffprobe -v quiet -print_format json -show_format -show_streams -loglevel error source.mp4

音频提取

从一个视频中提取音频

1
2
ffmpeg -i {0} -vn -y -acodec libfdk_aac {1} -loglevel error

也可以指定提取的音频时长

1
ffmpeg -i {0} -vn -y -acodec libfdk_aac -ss {1} -t {2} {3} -loglevel error

视频转码

1
2
ffmpeg -i b45245ccfedcaa6373ee4e4b36b47b98.mov -s 1280x720 -profile:v high444 -c:v libx264 -preset veryslow -crf 30 -r 30 -g 120 -keyint_min 30 -sc_threshold 40 -bf 3 -b_strategy 2 -refs 5 -c:a libfdk_aac -profile:a aac_low -b:a 128k -movflags faststart -max_muxing_queue_size 9999 -f mp4 -loglevel error 720_3.mp4 -y

文字水印(明水印)

logo水印(明水印)

斜水印(明水印)

1
2
3
4
5
ffmpeg -i {0} -f lavfi -i 'color={1}@0:s={2},format=yuva420p' -filter_complex '" \
"[1]trim=end_frame=1,drawtext=fontfile={3}:text={4}:fontsize={5}:fontcolor={6}:alpha={7}," \
"split[text][alpha];[text][alpha]alphamerge,rotate=({8}):ow=rotw({9}):oh=roth({10}):c=black@0," \
"loop=-1:1:0,tile=30x9,trim=end_frame=1[wm];[0][wm]overlay=0:0' -c:v libx264 -crf 25 " \
"-c:a libfdk_aac -profile:a aac_low -b:a 128k -movflags faststart -y {11} -loglevel error

数字水印(暗水印)

超分

截帧

超清

人像增强