#!perl

## no critic: TestingAndDebugging::RequireUseStrict

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2024-01-13'; # DATE
our $DIST = 'App-SubtitleUtils'; # DIST
our $VERSION = '0.013'; # VERSION

$secs_re = qr/[+-]?\d+(?:\.\d*)?/;
$hms_re = qr/\d\d?:\d\d?:\d\d?(?:,\d{1,3})?/;
$hms_re_catch = qr/(\d\d?):(\d\d?):(\d\d?)(?:,(\d{1,3}))?/;
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 <time1-in-srt> <time1-actual> <time2-srt> <time2-actual> FILE\n" unless @ARGV >= 4;

($time1_srt, $time1_actual, $time2_srt, $time2_actual) = splice @ARGV,0,4;

for ($time1_srt, $time1_actual, $time2_srt, $time2_actual) {
	if (/^$secs_re$/) {
		# already ok
	} elsif (/^$hms_re$/) {
		$_ = hms2secs($_);
	} else {
		die "FATAL: Invalid value `$_'\n";
	}
}

die "Please provide two different srt times!\n" if $time1_srt == $time2_srt;
die "Please provide two different actual times!\n" if $time1_actual == $time2_actual;

$scale = ($time2_actual-$time1_actual) / ($time2_srt-$time1_srt);
$shift = $time2_actual - $time2_srt*$scale; # time1 juga boleh

print STDERR "Adjusting: scale factor=$scale, shift=$shift secs.\n";

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

# ABSTRACT: Adjust (scale/shift) .srt by fitting two points in time
# PODNAME: srtadjust

__END__

=pod

=encoding UTF-8

=head1 NAME

srtadjust - Adjust (scale/shift) .srt by fitting two points in time

=head1 VERSION

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

=head1 SYNOPSIS

 % srtadjust <time1-in-srt> <time1-actual> <time2-srt> <time2-actual> [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-04-12 - first written.

2003-08-12 - realized that .srt can also contain coordinates. srtshift &
srtscale have not been fixed.

=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