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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import argparse
import textwrap
import logging
import os
import shlex
import json

try:
    from json.decoder import JSONDecodeError
except ImportError:
    JSONDecodeError = ValueError

from ._version import __version__
from ._ffmpeg_normalize import FFmpegNormalize, NORMALIZATION_TYPES
from ._errors import FFmpegNormalizeError
from ._logger import setup_custom_logger

logger = setup_custom_logger("ffmpeg_normalize")


def create_parser():
    parser = argparse.ArgumentParser(
        prog="ffmpeg-normalize",
        description=textwrap.dedent(
            """\
            ffmpeg-normalize v{} -- command line tool for normalizing audio files
            """.format(
                __version__
            )
        ),
        usage=textwrap.dedent(
            """\
            ffmpeg-normalize input [input ...]
                [-h]
                [-o OUTPUT [OUTPUT ...]] [-of OUTPUT_FOLDER]
                [-f] [-d] [-v] [-q] [-n] [-pr]
                [--version]
                [-nt {ebu,rms,peak}] [-t TARGET_LEVEL] [-p]
                [-lrt LOUDNESS_RANGE_TARGET] [-tp TRUE_PEAK] [--offset OFFSET] [--dual-mono]
                [-c:a AUDIO_CODEC] [-b:a AUDIO_BITRATE] [-ar SAMPLE_RATE] [-koa]
                [-prf PRE_FILTER] [-pof POST_FILTER]
                [-vn] [-c:v VIDEO_CODEC]
                [-sn] [-mn] [-cn]
                [-ei EXTRA_INPUT_OPTIONS] [-e EXTRA_OUTPUT_OPTIONS]
                [-ofmt OUTPUT_FORMAT]
                [-ext EXTENSION]
        """
        ),
        formatter_class=argparse.RawTextHelpFormatter,
        epilog=textwrap.dedent(
            """\
            The program additionally respects environment variables:

              - `TMP` / `TEMP` / `TMPDIR`
                    Sets the path to the temporary directory in which files are
                    stored before being moved to the final output directory.
                    Note: You need to use full paths.

              - `FFMPEG_PATH`
                    Sets the full path to an `ffmpeg` executable other than
                    the system default.


            Author: Werner Robitza
            License: MIT
            Homepage / Issues: https://github.com/slhck/ffmpeg-normalize
            """
        ),
    )

    group_io = parser.add_argument_group("File Input/output")
    group_io.add_argument("input", nargs="+", help="Input media file(s)")
    group_io.add_argument(
        "-o",
        "--output",
        nargs="+",
        help=textwrap.dedent(
            """\
        Output file names. Will be applied per input file.

        If no output file name is specified for an input file, the output files
        will be written to the default output folder with the name `<input>.<ext>`,
        where `<ext>` is the output extension (see `-ext` option).

        Example: ffmpeg-normalize 1.wav 2.wav -o 1n.wav 2n.wav
        """
        ),
    )
    group_io.add_argument(
        "-of",
        "--output-folder",
        type=str,
        help=textwrap.dedent(
            """\
            Output folder (default: `normalized`)

            This folder will be used for input files that have no explicit output
            name specified.
        """
        ),
        default="normalized",
    )

    group_general = parser.add_argument_group("General Options")
    group_general.add_argument(
        "-f", "--force", action="store_true", help="Force overwrite existing files"
    )
    group_general.add_argument(
        "-d", "--debug", action="store_true", help="Print debugging output"
    )
    group_general.add_argument(
        "-v", "--verbose", action="store_true", help="Print verbose output"
    )
    group_general.add_argument(
        "-q", "--quiet", action="store_true", help="Only print errors in output"
    )
    group_general.add_argument(
        "-n",
        "--dry-run",
        action="store_true",
        help="Do not run normalization, only print what would be done",
    )
    group_general.add_argument(
        "-pr",
        "--progress",
        action="store_true",
        help="Show progress bar for files and streams",
    )
    group_general.add_argument(
        "--version",
        action="version",
        version=f"%(prog)s v{__version__}",
        help="Print version and exit",
    )

    group_normalization = parser.add_argument_group("Normalization")
    group_normalization.add_argument(
        "-nt",
        "--normalization-type",
        type=str,
        choices=NORMALIZATION_TYPES,
        help=textwrap.dedent(
            """\
        Normalization type (default: `ebu`).

        EBU normalization performs two passes and normalizes according to EBU
        R128.

        RMS-based normalization brings the input file to the specified RMS
        level.

        Peak normalization brings the signal to the specified peak level.
        """
        ),
        default="ebu",
    )
    group_normalization.add_argument(
        "-t",
        "--target-level",
        type=float,
        help=textwrap.dedent(
            """\
        Normalization target level in dB/LUFS (default: -23).

        For EBU normalization, it corresponds to Integrated Loudness Target
        in LUFS. The range is -70.0 - -5.0.

        Otherwise, the range is -99 to 0.
        """
        ),
        default=-23.0,
    )
    group_normalization.add_argument(
        "-p",
        "--print-stats",
        action="store_true",
        help="Print first pass loudness statistics formatted as JSON to stdout",
    )

    # group_normalization.add_argument(
    #     '--threshold',
    #     type=float,
    #     help=textwrap.dedent("""\
    #     Threshold below which normalization should not be run.

    #     If the stream falls within the threshold, it will simply be copied.
    #     """),
    #     default=0.5
    # )

    group_ebu = parser.add_argument_group("EBU R128 Normalization")
    group_ebu.add_argument(
        "-lrt",
        "--loudness-range-target",
        type=float,
        help=textwrap.dedent(
            """\
        EBU Loudness Range Target in LUFS (default: 7.0).
        Range is 1.0 - 20.0.
        """
        ),
        default=7.0,
    )

    group_ebu.add_argument(
        "-tp",
        "--true-peak",
        type=float,
        help=textwrap.dedent(
            """\
        EBU Maximum True Peak in dBTP (default: -2.0).
        Range is -9.0 - +0.0.
        """
        ),
        default=-2.0,
    )

    group_ebu.add_argument(
        "--offset",
        type=float,
        help=textwrap.dedent(
            """\
        EBU Offset Gain (default: 0.0).
        The gain is applied before the true-peak limiter in the first pass only.
        The offset for the second pass will be automatically determined based on the first pass statistics.
        Range is -99.0 - +99.0.
        """
        ),
        default=0.0,
    )

    group_ebu.add_argument(
        "--dual-mono",
        action="store_true",
        help=textwrap.dedent(
            """\
        Treat mono input files as "dual-mono".

        If a mono file is intended for playback on a stereo system, its EBU R128
        measurement will be perceptually incorrect. If set, this option will
        compensate for this effect. Multi-channel input files are not affected
        by this option.
        """
        ),
    )

    group_acodec = parser.add_argument_group("Audio Encoding")
    group_acodec.add_argument(
        "-c:a",
        "--audio-codec",
        type=str,
        help=textwrap.dedent(
            """\
        Audio codec to use for output files.
        See `ffmpeg -encoders` for a list.

        Will use PCM audio with input stream bit depth by default.
        """
        ),
    )
    group_acodec.add_argument(
        "-b:a",
        "--audio-bitrate",
        type=str,
        help=textwrap.dedent(
            """\
        Audio bitrate in bits/s, or with K suffix.

        If not specified, will use codec default.
        """
        ),
    )
    group_acodec.add_argument(
        "-ar",
        "--sample-rate",
        type=str,
        help=textwrap.dedent(
            """\
        Audio sample rate to use for output files in Hz.

        Will use input sample rate by default, except for EBU normalization,
        which will change the input sample rate to 192 kHz.
        """
        ),
    )
    group_acodec.add_argument(
        "-koa",
        "--keep-original-audio",
        action="store_true",
        help="Copy original, non-normalized audio streams to output file",
    )
    group_acodec.add_argument(
        "-prf",
        "--pre-filter",
        type=str,
        help=textwrap.dedent(
            """\
        Add an audio filter chain before applying normalization.
        Multiple filters can be specified by comma-separating them.
        """
        ),
    )
    group_acodec.add_argument(
        "-pof",
        "--post-filter",
        type=str,
        help=textwrap.dedent(
            """\
        Add an audio filter chain after applying normalization.
        Multiple filters can be specified by comma-separating them.

        For EBU, the filter will be applied during the second pass.
        """
        ),
    )

    group_vcodec = parser.add_argument_group("Other Encoding Options")
    group_vcodec.add_argument(
        "-vn",
        "--video-disable",
        action="store_true",
        help="Do not write video streams to output",
    )
    group_vcodec.add_argument(
        "-c:v",
        "--video-codec",
        type=str,
        help=textwrap.dedent(
            """\
        Video codec to use for output files (default: 'copy').
        See `ffmpeg -encoders` for a list.

        Will attempt to copy video codec by default.
        """
        ),
        default="copy",
    )
    group_vcodec.add_argument(
        "-sn",
        "--subtitle-disable",
        action="store_true",
        help="Do not write subtitle streams to output",
    )
    group_vcodec.add_argument(
        "-mn",
        "--metadata-disable",
        action="store_true",
        help="Do not write metadata to output",
    )
    group_vcodec.add_argument(
        "-cn",
        "--chapters-disable",
        action="store_true",
        help="Do not write chapters to output",
    )

    group_format = parser.add_argument_group("Input/Output options")
    group_format.add_argument(
        "-ei",
        "--extra-input-options",
        type=str,
        help=textwrap.dedent(
            """\
        Extra input options list.

        A list of extra ffmpeg command line arguments valid for the input,
        applied before ffmpeg's `-i`.

        You can either use a JSON-formatted list (i.e., a list of
        comma-separated, quoted elements within square brackets), or a simple
        string of space-separated arguments.

        If JSON is used, you need to wrap the whole argument in quotes to
        prevent shell expansion and to preserve literal quotes inside the
        string. If a simple string is used, you need to specify the argument
        with `-e=`.

        Examples: `-e '[ "-f", "mpegts" ]'` or `-e="-f mpegts"`
        """
        ),
    )
    group_format.add_argument(
        "-e",
        "--extra-output-options",
        type=str,
        help=textwrap.dedent(
            """\
        Extra output options list.

        A list of extra ffmpeg command line arguments.

        You can either use a JSON-formatted list (i.e., a list of
        comma-separated, quoted elements within square brackets), or a simple
        string of space-separated arguments.

        If JSON is used, you need to wrap the whole argument in quotes to
        prevent shell expansion and to preserve literal quotes inside the
        string. If a simple string is used, you need to specify the argument
        with `-e=`.

        Examples: `-e '[ "-vbr", "3" ]'` or `-e="-vbr 3"`
        """
        ),
    )
    group_format.add_argument(
        "-ofmt",
        "--output-format",
        type=str,
        help=textwrap.dedent(
            """\
        Media format to use for output file(s).
        See 'ffmpeg -formats' for a list.

        If not specified, the format will be inferred by ffmpeg from the output
        file name. If the output file name is not explicitly specified, the
        extension will govern the format (see '--extension' option).
        """
        ),
    )
    group_format.add_argument(
        "-ext",
        "--extension",
        type=str,
        help=textwrap.dedent(
            """\
        Output file extension to use for output files that were not explicitly
        specified. (Default: `mkv`)
        """
        ),
        default="mkv",
    )
    return parser


def _split_options(opts):
    """
    Parse extra options (input or output) into a list
    """
    if not opts:
        return []
    try:
        if opts.startswith("["):
            try:
                ret = [str(s) for s in json.loads(opts)]
            except JSONDecodeError:
                ret = shlex.split(opts)
        else:
            ret = shlex.split(opts)
    except Exception as e:
        raise FFmpegNormalizeError(f"Could not parse extra_options: {e}")
    return ret


def main():
    cli_args = create_parser().parse_args()

    if cli_args.quiet:
        logger.setLevel(logging.ERROR)
    elif cli_args.debug:
        logger.setLevel(logging.DEBUG)
    elif cli_args.verbose:
        logger.setLevel(logging.INFO)

    # parse extra options
    extra_input_options = _split_options(cli_args.extra_input_options)
    extra_output_options = _split_options(cli_args.extra_output_options)

    ffmpeg_normalize = FFmpegNormalize(
        normalization_type=cli_args.normalization_type,
        target_level=cli_args.target_level,
        print_stats=cli_args.print_stats,
        loudness_range_target=cli_args.loudness_range_target,
        # threshold=cli_args.threshold,
        true_peak=cli_args.true_peak,
        offset=cli_args.offset,
        dual_mono=cli_args.dual_mono,
        audio_codec=cli_args.audio_codec,
        audio_bitrate=cli_args.audio_bitrate,
        sample_rate=cli_args.sample_rate,
        keep_original_audio=cli_args.keep_original_audio,
        pre_filter=cli_args.pre_filter,
        post_filter=cli_args.post_filter,
        video_codec=cli_args.video_codec,
        video_disable=cli_args.video_disable,
        subtitle_disable=cli_args.subtitle_disable,
        metadata_disable=cli_args.metadata_disable,
        chapters_disable=cli_args.chapters_disable,
        extra_input_options=extra_input_options,
        extra_output_options=extra_output_options,
        output_format=cli_args.output_format,
        dry_run=cli_args.dry_run,
        progress=cli_args.progress,
    )

    if (
        cli_args.output is not None
        and len(cli_args.output) > 0
        and len(cli_args.input) > len(cli_args.output)
    ):
        logger.warning(
            "There are more input files than output file names given. "
            "Please specify one output file name per input file using -o <output1> <output2> ... "
            "Will apply default file naming for the remaining ones."
        )

    for index, input_file in enumerate(cli_args.input):
        if cli_args.output is not None and index < len(cli_args.output):
            if cli_args.output_folder and cli_args.output_folder != "normalized":
                logger.warning(
                    "Output folder {} is ignored for input file {}".format(
                        cli_args.output_folder, input_file
                    )
                )
            output_file = cli_args.output[index]
            output_dir = os.path.dirname(output_file)
            if output_dir != "" and not os.path.isdir(output_dir):
                raise FFmpegNormalizeError(
                    f"Output file path {output_dir} does not exist"
                )
        else:
            output_file = os.path.join(
                cli_args.output_folder,
                os.path.splitext(os.path.basename(input_file))[0]
                + "."
                + cli_args.extension,
            )
            if not os.path.isdir(cli_args.output_folder) and not cli_args.dry_run:
                logger.warning(
                    "Output directory '{}' does not exist, will create".format(
                        cli_args.output_folder
                    )
                )
                os.makedirs(cli_args.output_folder)

        if os.path.exists(output_file) and not cli_args.force:
            logger.error(
                "Output file {} already exists, skipping. Use -f to force overwriting.".format(
                    output_file
                )
            )
        else:
            ffmpeg_normalize.add_media_file(input_file, output_file)

    ffmpeg_normalize.run_normalization()


if __name__ == "__main__":
    main()