aboutsummaryrefslogtreecommitdiff
path: root/vcrop
blob: 4703cbc006da30b4656a7a707589c669d1ccf3cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env perl

use v5.36;
use strict;
use warnings;

use File::Basename qw<fileparse>;
use File::Temp qw<tempdir>;

sub usage() {
    die "Usage: $0 <file|url> crop1 [, crop2 ...]\n";
}

sub crop($file, $crop) {
    my ($from, $x, $y, $plus, $to) = $crop =~ /^(\d+(:\d{1,2}){0,2})(-(\+)?(\d+(:\d{1,2}){0,2}))?$/;
    unless ($from) {
        print "Error: invalid crop '$crop' (ignoring)\n";
        return;
    }

    my ($file_noext, $file_dir, $file_ext) = fileparse($file, qr/\.[^.]*/);
    my $to_suffix = defined $to ? "_$to" : "";
    my $cropfile = "${file_noext}_${from}${to_suffix}$file_ext";
    if (defined $to && defined $plus) {
        `ffmpeg -loglevel error -i "$file" -ss "$from" -t "${to}s" -c:v copy -c:a copy "$cropfile"`
    } elsif (defined $to) {
        `ffmpeg -loglevel error -i "$file" -ss "$from" -to "$to" -c:v copy -c:a copy "$cropfile"`
    } else {
        `ffmpeg -loglevel error -i "$file" -ss "$from" -t 15s -c:v copy -c:a copy "$cropfile"`
    }
    print "Cropped to '$cropfile'\n";
}

@ARGV > 1 or usage;
my $src = shift;
my @crops = @ARGV;

# Compute $file (either from arg or downloading it beforehand)
my $file;
if ($src =~ /^http/) {
    print "Downloading '$src'...\n";
    my $tmpdir = tempdir(CLEANUP => 1);
    $file = `yt-dlp -P "$tmpdir" -q "$src" --exec 'echo "%(filepath)s"'` or die $!;
    chomp $file;
    print "Saved to '$file'\n";
} else {
    $file = $src;
}
die "ERROR: File '$file' does not exist.\n" unless -f $file;

# Make crops
foreach my $crop (@crops) {
    crop($file, $crop);
}