#!perl

## no critic: TestingAndDebugging::RequireUseStrict

# bbrp scale factor yang umum:
# - 1.042709376 (25/23.976): PAL -> NTSC/FILM
# - 0.8341675008 (25/29.97): PAL -> NTSC
#
# scale dulu, baru shift (kalau perlu)

$secs_re = qr/[+-]?\d+(?:\.\d*)?/;
$hms_re = qr/\d\d?:\d\d?:\d\d?(?:,\d\d\d)?/;
$hms_re_catch = qr/(\d\d?):(\d\d?):(\d\d?)(?:,(\d\d\d))?/;
sub hms2secs { local $_=shift; /^$hms_re_catch$/ or return; $1*3600+$2*60+$3+$4*0.001 }
sub secs2hms { local $_=shift; /^$secs_re$/ or return "00:00:00,000"; my $ms=1000*($_-int($_)); $_=int($_); my $s=$_%60; $_-=$s; $_/=60; my $m=$_%60; $_-=$m; $_/=60; sprintf "%02d:%02d:%02d,%03d",$_,$m,$s,$ms }

###

$|++;
die "Usage: $0 factor FILE\nFactor of 2.0 equals to twice longer.\n" unless @ARGV >= 1;
$_ = shift @ARGV;
if (/^\d+(\.\d*)?$/) {
	$f = $_;
} else {
	die "FATAL: Invalid factor `$_'\n";
}

$para = "";
while (1) {
	$_ = <>;
	if (/\S/) {
		s/\015//g;
		$para .= $_;
	} elsif ($para =~ /\S/) {
		($no, $hms1, $hms2, $text) = $para =~ /(\d+)\n($hms_re) ---?> ($hms_re)\n(.+)/s or
			die "FATAL: Invalid entry in line $.: $para\n";
		print $no, "\n",
			secs2hms(hms2secs($hms1)*$f), " --> ", secs2hms(hms2secs($hms2)*$f), "\n",
			$text, "\n";
		$para = "";
	} else {
		$para = "";
	}
	last unless $_;
}

# ABSTRACT: Speed up or slow down times in .srt by a factor
# PODNAME: srtscale

__END__

=pod

=encoding UTF-8

=head1 NAME

srtscale - Speed up or slow down times in .srt by a factor

=head1 VERSION

This document describes version 0.013 of srtscale (from Perl distribution App-SubtitleUtils), released on 2024-01-13.

=head1 SYNOPSIS

 % srtscale <factor> [FILE] ...

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-SubtitleUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-SubtitleUtils>.

=head1 HISTORY

2003-02-06 - first written

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 CONTRIBUTING


To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.

Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
that are considered a bug and can be reported to me.

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2024, 2022, 2021, 2020 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-SubtitleUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=cut