#!/usr/bin/perl

use strict;
use warnings;

use DBI;
use File::Path qw/make_path/;
use SReview::Talk;
use Media::Convert::Asset;
use Media::Convert::Pipe;
use Media::Convert::Asset::ProfileFactory;
use SReview::Config::Common;
use SReview::Files::Factory;

=head1 NAME

sreview-previews - create previews from the C<sreview-cut> output

=head1 SYNOPSIS

sreview-previews TALKID

=head1 DESCRIPTION

C<sreview-previews> performs the following actions:

=over

=item *

Look up the talk with id TALKID in the database.

=item *

Verify if the codecs in the pre, main, and post videos as produced by
L<sreview-cut> are HTML5-compatible. If they are, copy them to a MP4
or WebM container from the Matroska one.

=item *

If they are not, convert them to the C<vp8_lq> profile

=item *

Update the database to set the current talk's C<progress> field to
C<done>.

=back

=head1 CONFIGURATION

C<sreview-previews> considers the following configuration values:

=over

=cut

my $config = SReview::Config::Common::setup;
my $collection = SReview::Files::Factory->create("intermediate", $config->get("pubdir"));

sub convert($) {
	my $relname = shift;
	return unless ($collection->has_file($relname . ".mkv"));
	my $input_file = $collection->get_file(relname => $relname . ".mkv");
	my $input = Media::Convert::Asset->new(url => $input_file->filename);
	my $vc = $input->video_codec;
	my $ac = $input->audio_codec;

	if (!$config->get("force_preview_transcode")) {
		if (($vc eq "vp8" && $ac eq "vorbis") || ($vc eq "vp9" && $ac eq "vorbis") || ($vc eq "vp9" && $ac eq "opus")) {
			my $output_file = $collection->add_file(relname => $relname . ".webm");
			my $output = Media::Convert::Asset->new(url => $output_file->filename);
			Media::Convert::Pipe->new(inputs => [$input], output => $output, vcopy => 1, acopy => 1)->run();
			$output_file->store_file;
			return;
		}
		if ($vc eq "h264" && $ac eq "aac") {
			my $output_file = $collection->add_file(relname => $relname . ".mp4");
			my $output = Media::Convert::Asset->new(url => $output_file->filename);
			Media::Convert::Pipe->new(inputs => [$input], output => $output, vcopy => 1, acopy => 1)->run();
			$output_file->store_file;
			return;
		}
	}
	my $profile = Media::Convert::Asset::ProfileFactory->create('vp8_lq', $input, $config->get('extra_profiles'));
	my $output_file = $collection->add_file(relname => $relname . ".webm");
	my $output = Media::Convert::Asset->new(url => $output_file->filename, reference => $profile);
	Media::Convert::Pipe->new(inputs => [$input], output => $output)->run();
	$output_file->store_file;
}

=item dbistring

The DBI string used to connect to the database.

=cut

my $dbh = DBI->connect($config->get('dbistring'), '', '') or die "Cannot connect to database!";
my $talkid = $ARGV[0];

$dbh->prepare("UPDATE talks SET progress='running', state='generating_previews' WHERE id=?")->execute($talkid);

my $talk = SReview::Talk->new(talkid => $talkid);

=item pubdir

The directory in which to find the output of C<sreview-cut>, and in
which to write the previews

=cut

my $relname = $talk->relative_name;

convert($relname . "/pre");
convert($relname . "/main");
convert($relname . "/post");

$dbh->prepare("UPDATE talks SET progress='done' WHERE id=? AND state='generating_previews'")->execute($talkid);

=back

=head1 SEE ALSO

C<sreview-cut>, C<sreview-transcode>, C<sreview-skip>, C<sreview-config>

=cut
