Skip to content

Commit

Permalink
Add mucho logging
Browse files Browse the repository at this point in the history
It's easier to understand what sreview-cut does (or does wrong) if it is
a bit more chatty, so make it do that.
  • Loading branch information
yoe committed Feb 6, 2025
1 parent eced1a5 commit 7c9a32f
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions scripts/sreview-cut
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use Media::Convert::Pipe;;
use SReview::Config::Common;
use SReview::Files::Factory;
use SReview::Talk;
use JSON::MaybeXS;

=head1 NAME
Expand Down Expand Up @@ -207,8 +208,21 @@ my $output_coll = SReview::Files::Factory->create("intermediate", $config->get("

$output_coll->delete_files(relnames => [dirname($talk->relative_name)]);

my $fragments_title = encode_json($talk->avs_video_fragments);

say "Video fragments:"
say "================"

say $fragments_title;
say "-" x length($fragments_title);

FRAGMENT:
foreach my $row(@{$talk->avs_video_fragments}) {
next if($row->{raw_length_corrected} <= 0.5 && $row->{talkid} < 0);
say "Considering work on " . $row->{raw_filename};
if($row->{raw_length_corrected} <= 0.5 && $row->{talkid} < 0) {
say "skipping, file length less than half a second";
next FRAGMENT;
}
my $start;
my $stop;
my $target;
Expand Down Expand Up @@ -236,6 +250,7 @@ foreach my $row(@{$talk->avs_video_fragments}) {
my ($numerator, $denumerator) = split /\//, $rate;
my $frame_length = 1 / ($numerator / $denumerator);
if($row->{fragment_start} != 0) {
say "The wanted fragment does not start at the beginning, considering what to do";
my $prev_kf = 0;
my $cur_kf = 0;
KF:
Expand All @@ -245,8 +260,7 @@ foreach my $row(@{$talk->avs_video_fragments}) {
$prev_kf = $kf;
}
if($prev_kf != $row->{fragment_start}) {
# The start point does not fall on a key frame. Handle
# the situation
say "The desired cut point is not on a key frame, deciding how to handle the situation";
my $profile;
if(defined($config->get("input_profile"))) {
$profile = Media::Convert::Asset::ProfileFactory->create($config->get("input_profile"), $input, $config->get("extra_profiles"));
Expand All @@ -257,8 +271,10 @@ foreach my $row(@{$talk->avs_video_fragments}) {
# Cut point is less than one frame before the
# next key frame. Move the cut point to the key
# frame.
say "The desired cut point is less than one frame before the next key frame. Moving the cut point to the key frame.";
$output->fragment_start($cur_kf);
} else {
say "The desired cut point does not fall on or near a key frame. Creating a new key frame at the desired cut point.";
# The start point is reasonably far away from a
# key frame. This means we need to create one,
# otherwise the video does not start correctly,
Expand Down Expand Up @@ -286,11 +302,16 @@ foreach my $row(@{$talk->avs_video_fragments}) {
$target_length -= $temp->duration;
}
} else {
say "The desired cut point is on a key frame. Doing a dumb copy.";
$output->fragment_start($row->{fragment_start});
}
}
} else {
say "The wanted fragment starts at the beginning of the file. Doing a dumb copy.";
}
if($row->{raw_length} ne $row->{raw_length_corrected}) {
say "The desired duration is $target_length";
if($target_length < $frame_length) {
say "This is less than a frame, skipping this fragment.";
next FRAGMENT;
}
$output->duration($target_length);
Expand All @@ -317,13 +338,16 @@ my $post_new = undef;
my @videos;

if(defined($pre)) {
say "Concatenating pre fragments";
$pre_new = Media::Convert::Asset->new(url => "$tempdir/pre.mkv", %duration_args);
Media::Convert::Pipe->new(inputs => [$pre], output => $pre_new, map => [Media::Convert::Map->new(input => $pre, type => "allcopy")], vcopy => 1, acopy => 1)->run();
push @videos, $pre_new;
}
say "Concatenating main fragments";
Media::Convert::Pipe->new(inputs => [$main], output => $main_new, map => [Media::Convert::Map->new(input => $main, type => "allcopy")], vcopy => 1, acopy => 1)->run();
push @videos, $main;
if(defined($post)) {
say "Concatenating post fragments";
$post_new = Media::Convert::Asset->new(url => "$tempdir/post.mkv", %duration_args);
Media::Convert::Pipe->new(inputs => [$post], output => $post_new, map => [Media::Convert::Map->new(input => $post, type => "allcopy")], vcopy => 1, acopy => 1)->run();
push @videos, $post_new;
Expand All @@ -349,24 +373,28 @@ sub reload {

reload();

say "Extracting audio samples";
my $samplestart = ($main->duration / 2) - 30;
my $samplelen = 60;
$samplestart = ($samplestart > 0) ? $samplestart : 0;
$samplelen = ($samplelen > $mainlen) ? $mainlen : $samplelen;
my @choices = ($primary_audio, $backup_audio, $both_audio);
foreach my $stream(0, 1, 2) {
say "stream $stream wav";
my $sample_wav_file = $output_coll->add_file(relname => $talk->relative_name . "/audio$stream.wav");
my $sample_wav = Media::Convert::Asset->new(url => $sample_wav_file->filename, fragment_start => $samplestart, duration => $samplelen, %duration_args);
Media::Convert::Pipe->new(inputs => [$main], output => $sample_wav, map => [Media::Convert::Map->new(input => $main, type => $maptype, choice => $choices[$stream])], acopy => 0, vskip => 1)->run();
$sample_wav_file->store_file;
foreach my $codec(qw/mp3 ogg/) {
say "stream $stream $codec";
my $tmp = $output_coll->add_file(relname => $talk->relative_name . "/audio$stream.$codec");
Media::Convert::Pipe->new(inputs => [$sample_wav], output => Media::Convert::Asset->new(url => $tmp->filename, %duration_args), acopy => 0, vskip => 1)->run();
$tmp->store_file;
}
}

if($corrections->{offset_audio} != 0) {
say "Performing A/V sync correction";
for my $vid(@videos) {
my $tmp = Media::Convert::Asset->new(url => "$tempdir/temp.mkv", %duration_args);
Media::Convert::AvSync->new(input => $vid, output => $tmp, audio_delay => $corrections->{offset_audio})->run();
Expand All @@ -376,6 +404,7 @@ if($corrections->{offset_audio} != 0) {
}

if(!$talk->get_flag("keep_audio")) {
say "Performing audio normalization";
my $tmp = Media::Convert::Asset->new(url => "$tempdir/channel.mkv", audio_codec => $main->audio_codec, %duration_args);
Media::Convert::Pipe->new(inputs => [$main], output => $tmp, map => [Media::Convert::Map->new(input => $main, type => $maptype, choice => $choices[$corrections->{audio_channel}])], acopy => 0, vskip => 1)->run();
my $tmp2 = Media::Convert::Asset->new(url => "$tempdir/merged.mkv", %duration_args);
Expand All @@ -396,6 +425,7 @@ if(!$talk->get_flag("keep_audio")) {
}
}

say "Done, updating database and uploading files if necessary";
my $postlen_db = 0;
my $prelen_db = 0;
if(defined($pre)) {
Expand Down

0 comments on commit 7c9a32f

Please sign in to comment.