@@ -5,6 +5,39 @@ using JSON
5
5
const preroll = ARGS [1 ]
6
6
const file = ARGS [2 ]
7
7
8
+ const target_il = - 24.0
9
+ const target_lra = + 11.0
10
+ const target_tp = - 2.0
11
+
12
+ const loudnorm_query = " loudnorm=dual_mono=true:I=$(target_il) :LRA=$(target_lra) :tp=$(target_tp) "
13
+
14
+ function query_loudness (file)
15
+ output_lines = FFMPEG. exe (` -i $file -filter:a $(loudnorm_query* " :print_format=json" ) -vn -sn -f null NULL` , collect= true )
16
+
17
+ loudnorm_start = nothing
18
+ loudnorm_end = nothing
19
+ for (i,line) in enumerate (output_lines)
20
+ if startswith (line, " [Parsed_loudnorm" )
21
+ loudnorm_start = i + 1
22
+ continue
23
+ end
24
+
25
+ if loudnorm_start != = nothing && startswith (line, " }" )
26
+ loudnorm_end = i
27
+ continue
28
+ end
29
+ end
30
+
31
+ if loudnorm_end === nothing || loudnorm_start === nothing
32
+ error (" Failed to parse ffmpeg output" )
33
+ end
34
+
35
+ json = join (output_lines[loudnorm_start: loudnorm_end], " \n " )
36
+ properties = JSON. Parser. parse (json)
37
+ end
38
+
39
+ format_loudnorm (p) = loudnorm_query * " :linear=true:measured_I=$(p[" input_i" ]) :measured_LRA=$(p[" input_lra" ]) :measured_tp=$(p[" input_tp" ]) :measured_thresh=$(p[" input_thresh" ]) :offset=$(p[" target_offset" ]) "
40
+
8
41
# 1. Query properties of file
9
42
10
43
json = join (FFMPEG. exe (` -v error -show_entries stream=width,height -of json $(file) ` , command= FFMPEG. ffprobe, collect= true ), " \n " )
@@ -17,13 +50,22 @@ sizes = map(stream -> (stream["width"], stream["height"]), streams)
17
50
unique! (sizes)
18
51
width, height = only (sizes)
19
52
53
+ @sync begin
54
+ global query0 = @async query_loudness (preroll)
55
+ global query1 = @async query_loudness (file)
56
+ end
57
+ audio0_properties = fetch (query0)
58
+ audio1_properties = fetch (query1)
59
+
20
60
args = ```
21
61
-i $(preroll)
22
62
-i $(file)
23
63
-filter_complex
24
64
"[0:v]scale=$(width) :$(height) :force_original_aspect_ratio=decrease,pad=$(width) :$(height) :-1:-1,setsar=1,fps=30,format=yuv420p[v0]; \
25
65
[1:v]setsar=1,fps=30,format=yuv420p[v1];
26
- [v0][0:a][v1][1:a]concat=n=2:v=1:a=1[v][a]"
66
+ [0:a]$(format_loudnorm (audio0_properties)) [a0];
67
+ [1:a]$(format_loudnorm (audio1_properties)) [a1];
68
+ [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]"
27
69
-map "[v]"
28
70
-map "[a]"
29
71
-c:v libx264
@@ -35,6 +77,4 @@ args = ```
35
77
out/$(file)
36
78
```
37
79
38
- @show args
39
80
FFMPEG. exe (args)
40
-
0 commit comments