(
args: argparse.Namespace, reference_pipe: Optional[Pipeline], result: Dict[str, Any]
)
| 175 | |
| 176 | |
| 177 | def try_sync( |
| 178 | args: argparse.Namespace, reference_pipe: Optional[Pipeline], result: Dict[str, Any] |
| 179 | ) -> bool: |
| 180 | result["sync_was_successful"] = False |
| 181 | sync_was_successful = True |
| 182 | logger.info( |
| 183 | "extracting speech segments from %s...", |
| 184 | "stdin" if not args.srtin else "subtitles file(s) {}".format(args.srtin), |
| 185 | ) |
| 186 | if not args.srtin: |
| 187 | args.srtin = [None] |
| 188 | for srtin in args.srtin: |
| 189 | try: |
| 190 | skip_sync = args.skip_sync or reference_pipe is None |
| 191 | skip_infer_framerate_ratio = ( |
| 192 | args.skip_infer_framerate_ratio or reference_pipe is None |
| 193 | ) |
| 194 | srtout = _resolve_srtout(args, srtin) |
| 195 | srt_pipe_maker = get_srt_pipe_maker(args, srtin) |
| 196 | framerate_ratios = get_framerate_ratios_to_try(args) |
| 197 | srt_pipes = [srt_pipe_maker(1.0)] + [ |
| 198 | srt_pipe_maker(rat) for rat in framerate_ratios |
| 199 | ] |
| 200 | for srt_pipe in srt_pipes: |
| 201 | if callable(srt_pipe): |
| 202 | continue |
| 203 | else: |
| 204 | srt_pipe.fit(srtin) |
| 205 | if ( |
| 206 | not skip_infer_framerate_ratio |
| 207 | and hasattr(reference_pipe[-1], "num_frames") |
| 208 | and reference_pipe[-1].num_frames is not None |
| 209 | ): |
| 210 | inferred_framerate_ratio_from_length = ( |
| 211 | float(reference_pipe[-1].num_frames) |
| 212 | / cast(Pipeline, srt_pipes[0])[-1].num_frames |
| 213 | ) |
| 214 | logger.info( |
| 215 | "inferred frameratio ratio: %.3f" |
| 216 | % inferred_framerate_ratio_from_length |
| 217 | ) |
| 218 | srt_pipes.append( |
| 219 | cast( |
| 220 | Pipeline, srt_pipe_maker(inferred_framerate_ratio_from_length) |
| 221 | ).fit(srtin) |
| 222 | ) |
| 223 | logger.info("...done") |
| 224 | logger.info("computing alignments...") |
| 225 | if skip_sync: |
| 226 | best_score = 0.0 |
| 227 | best_srt_pipe = cast(Pipeline, srt_pipes[0]) |
| 228 | offset_samples = 0 |
| 229 | else: |
| 230 | (best_score, offset_samples), best_srt_pipe = MaxScoreAligner( |
| 231 | FFTAligner, srtin, SAMPLE_RATE, args.max_offset_seconds |
| 232 | ).fit_transform( |
| 233 | reference_pipe.transform(args.reference), |
| 234 | srt_pipes, |
no test coverage detected