|
1 |
| -import subprocess |
2 |
| - |
3 | 1 | from argparse import ArgumentParser
|
4 | 2 | from datetime import timedelta
|
5 | 3 | from pathlib import Path
|
@@ -46,30 +44,6 @@ def report_stats(times: Tensor, unit: str = "ms", prefix: str = "") -> float:
|
46 | 44 | )
|
47 | 45 |
|
48 | 46 |
|
49 |
| -def get_duration(path: Path) -> str: |
50 |
| - try: |
51 |
| - result = subprocess.run( |
52 |
| - [ |
53 |
| - "ffprobe", |
54 |
| - "-v", |
55 |
| - "error", |
56 |
| - "-show_entries", |
57 |
| - "format=duration", |
58 |
| - "-of", |
59 |
| - "default=noprint_wrappers=1:nokey=1", |
60 |
| - str(path), |
61 |
| - ], |
62 |
| - stdout=subprocess.PIPE, |
63 |
| - stderr=subprocess.PIPE, |
64 |
| - text=True, |
65 |
| - ) |
66 |
| - |
67 |
| - # Remove microseconds |
68 |
| - return str(timedelta(seconds=float(result.stdout.strip()))).split(".")[0] |
69 |
| - except Exception: |
70 |
| - return "?" |
71 |
| - |
72 |
| - |
73 | 47 | def decode_with_torchcodec(path: Path) -> None:
|
74 | 48 | AudioDecoder(path).get_all_samples()
|
75 | 49 |
|
@@ -97,25 +71,31 @@ def decode_with_torchaudio_load(path: Path, backend: str) -> None:
|
97 | 71 | args = parser.parse_args()
|
98 | 72 | path = Path(args.path)
|
99 | 73 |
|
| 74 | +metadata = AudioDecoder(path).metadata |
| 75 | +duration = str(timedelta(seconds=metadata.duration_seconds_from_header)).split(".")[0] |
100 | 76 |
|
101 | 77 | print(
|
102 |
| - f"Benchmarking {path.name}, duration: {get_duration(path)}, averaging over {args.num_exp} runs:" |
| 78 | + f"Benchmarking {path.name}, duration: {duration}, codec: {metadata.codec}, format: {metadata.sample_format}, averaging over {args.num_exp} runs:" |
103 | 79 | )
|
104 | 80 |
|
105 |
| -times = bench(decode_with_torchcodec, path, num_exp=args.num_exp) |
106 |
| -report_stats(times, prefix="torchcodec.AudioDecoder") |
| 81 | +for decode_f, kwargs, prefix in ( |
| 82 | + (decode_with_torchcodec, {}, "torchcodec.AudioDecoder"), |
| 83 | + ( |
| 84 | + decode_with_torchaudio_load, |
| 85 | + {"backend": "ffmpeg"}, |
| 86 | + "torchaudio.load(backend='ffmpeg')", |
| 87 | + ), |
| 88 | + (decode_with_torchaudio_load, {"backend": "sox"}, "torchaudio.load(backend='sox')"), |
| 89 | + ( |
| 90 | + decode_with_torchaudio_load, |
| 91 | + {"backend": "soundfile"}, |
| 92 | + "torchaudio.load(backend='soundfile')", |
| 93 | + ), |
| 94 | + (decode_with_torchaudio_StreamReader, {}, "torchaudio.StreamReader"), |
| 95 | +): |
107 | 96 |
|
108 |
| -times = bench(decode_with_torchaudio_load, path, backend="ffmpeg", num_exp=args.num_exp) |
109 |
| -report_stats(times, prefix="torchaudio.load(backend='ffmpeg')") |
110 |
| - |
111 |
| -prefix = "torchaudio.load(backend='sox')" |
112 |
| -try: |
113 |
| - times = bench( |
114 |
| - decode_with_torchaudio_load, path, backend="sox", num_exp=args.num_exp |
115 |
| - ) |
116 |
| - report_stats(times, prefix=prefix) |
117 |
| -except RuntimeError: |
118 |
| - print(f"{prefix:<40} Not supported") |
119 |
| - |
120 |
| -times = bench(decode_with_torchaudio_StreamReader, path, num_exp=args.num_exp) |
121 |
| -report_stats(times, prefix="torchaudio.StreamReader") |
| 97 | + try: |
| 98 | + times = bench(decode_f, path, **kwargs, num_exp=args.num_exp) |
| 99 | + report_stats(times, prefix=prefix) |
| 100 | + except RuntimeError: |
| 101 | + print(f"{prefix:<40} Not supported") |
0 commit comments