2 커밋 3b0b9b1ebc ... fe298b26cb

작성자 SHA1 메시지 날짜
  trizen fe298b26cb - CLI: added the `download_in_subdir` and `download_in_subdir_format` config-options. 3 주 전
  trizen 2ab328a70b - CLI: normalize the filename after applying the formatting. 3 주 전
1개의 변경된 파일63개의 추가작업 그리고 24개의 파일을 삭제
  1. 63 24
      bin/pipe-viewer

+ 63 - 24
bin/pipe-viewer

@@ -15,7 +15,7 @@
 #-------------------------------------------------------
 #  pipe-viewer
 #  Fork: 30 October 2020
-#  Edit: 11 August 2024
+#  Edit: 25 August 2024
 #  https://github.com/trizen/pipe-viewer
 #-------------------------------------------------------
 
@@ -284,6 +284,9 @@ my %CONFIG = (
     download_and_play  => 0,
     remove_played_file => 0,
 
+    download_in_subdir        => 0,               # true to download in a subfolder
+    download_in_subdir_format => '*AUTHOR*',
+
     bypass_age_gate_native     => 0,
     bypass_age_gate_with_proxy => 0,
     ignored_projections        => [],
@@ -357,7 +360,7 @@ my %CONFIG = (
     merge_with_captions => 1,
     set_mtime           => $constant{win32} ^ 1,
 
-    video_filename_format => '*FTITLE* - *ID*.*FORMAT*',
+    video_filename_format => '*TITLE* - *ID*.*FORMAT*',
 );
 
 local $SIG{__WARN__} = sub { warn @_; ++$opt{_error} };
@@ -906,7 +909,8 @@ usage: $execname [options] ([url] | [keywords])
    -d  --download!       : activate the download mode
    -dp --dl-play!        : play the video after download (with -d)
    -rp --rem-played!     : delete a local video after played (with -dp)
-       --wget-dl!        : download videos with wget (recommended)
+       --dl-in-subdir!   : download videos in subdirectories (with -d)
+       --wget-dl!        : download videos with wget
        --skip-if-exists! : don't download videos which already exist (with -d)
        --copy-caption!   : copy and rename the caption for downloaded videos
        --downloads-dir=s : downloads directory (set: '$opt{downloads_dir}')
@@ -1767,6 +1771,7 @@ sub parse_arguments {
         'video-info!'   => \$opt{show_video_info},
 
         'dp|downl-play|download-and-play|dl-play!' => \$opt{download_and_play},
+        'download-in-subdir|dl-in-subdir!'         => \$opt{download_in_subdir},
 
         'thousand-separator=s'           => \$opt{thousand_separator},
         'get-captions|get_captions!'     => \$opt{get_captions},
@@ -3686,13 +3691,15 @@ sub download_from_url {
 sub download_video {
     my ($streaming, $info) = @_;
 
-    my $video_filename = $yv_utils->format_text(
-                                                streaming => $streaming,
-                                                info      => $info,
-                                                text      => $opt{video_filename_format},
-                                                escape    => 0,
-                                                fat32safe => $opt{fat32safe},
-                                               );
+    my $video_filename = $yv_utils->normalize_filename(
+                                                       $yv_utils->format_text(
+                                                                              streaming => $streaming,
+                                                                              info      => $info,
+                                                                              text      => $opt{video_filename_format},
+                                                                              escape    => 0,
+                                                                             ),
+                                                       $opt{fat32safe}
+                                                      );
 
     $video_filename =~ s/\h*:+\h*/ - /g;    # replace colons (":") with dashes ("-")
 
@@ -3719,23 +3726,41 @@ sub download_video {
         $audio_info->{_youtube_url} = $video_info->{_youtube_url};
     }
 
-    if (not -d $opt{downloads_dir}) {
+    my $downloads_dir = $opt{downloads_dir};
+
+    # Download in subdirectory
+    if ($opt{download_in_subdir}) {
+        my $downloads_subdir = $yv_utils->normalize_filename(
+                                                             $yv_utils->format_text(
+                                                                                    streaming => $streaming,
+                                                                                    info      => $info,
+                                                                                    text      => $opt{download_in_subdir_format},
+                                                                                    escape    => 0,
+                                                                                   ),
+                                                             $opt{fat32safe}
+                                                            );
+
+        $downloads_dir = catdir($downloads_dir, $downloads_subdir);
+    }
+
+    # Create the downloads directory, when it doesn't exist
+    if (not -d $downloads_dir) {
         require File::Path;
-        if (not eval { File::Path::make_path($opt{downloads_dir}) }) {
-            warn colored("\n[!] Can't create directory '$opt{downloads_dir}': $1", 'bold red') . "\n";
+        if (not eval { File::Path::make_path($downloads_dir) }) {
+            warn colored("\n[!] Can't create directory <<$downloads_dir>>: $1", 'bold red') . "\n";
         }
     }
 
-    if (not -d $opt{downloads_dir}) {
-        warn colored("\n[!] Can't write into directory '$opt{downloads_dir}': $!", 'bold red') . "\n";
-        $opt{downloads_dir} = (-d curdir()) ? curdir() : (-d $ENV{HOME}) ? $ENV{HOME} : return;
-        warn colored("[!] Video will be downloaded into directory: $opt{downloads_dir}", 'bold red') . "\n";
+    if (not -d $downloads_dir) {
+        warn colored("\n[!] Can't write into directory <<$downloads_dir>>: $!", 'bold red') . "\n";
+        $downloads_dir = (-d curdir()) ? curdir() : (-d $ENV{HOME}) ? $ENV{HOME} : return;
+        warn colored("[!] Video will be downloaded into directory: $downloads_dir", 'bold red') . "\n";
     }
 
-    $mkv_filename   = catfile($opt{downloads_dir}, $mkv_filename);
-    $srt_filename   = catfile($opt{downloads_dir}, $srt_filename);
-    $audio_filename = catfile($opt{downloads_dir}, $audio_filename);
-    $video_filename = catfile($opt{downloads_dir}, $video_filename);
+    $mkv_filename   = catfile($downloads_dir, $mkv_filename);
+    $srt_filename   = catfile($downloads_dir, $srt_filename);
+    $audio_filename = catfile($downloads_dir, $audio_filename);
+    $video_filename = catfile($downloads_dir, $video_filename);
 
     if ($opt{skip_if_exists} and -e $mkv_filename) {
         $video_filename = $mkv_filename;
@@ -3812,7 +3837,7 @@ sub download_video {
     # Convert the downloaded video
     if (defined $opt{convert_to}) {
 
-        my $convert_filename = catfile($opt{downloads_dir}, "$naked_filename.$opt{convert_to}");
+        my $convert_filename = catfile($downloads_dir, "$naked_filename.$opt{convert_to}");
         my $convert_cmd      = $opt{convert_cmd};
 
         my %table = (
@@ -3834,7 +3859,7 @@ sub download_video {
 
             if (not $opt{keep_original_video}) {
                 unlink $video_filename
-                  or warn colored("\n[!] Can't unlink file '$video_filename': $!", 'bold red') . "\n\n";
+                  or warn colored("\n[!] Can't unlink file <<$video_filename>>: $!", 'bold red') . "\n\n";
             }
 
             $video_filename = $convert_filename if -e $convert_filename;
@@ -4957,6 +4982,20 @@ Directory where to download files and where to save converted files.
 
 Play downloaded videos.
 
+=head2 download_in_subdir
+
+Download videos in a new subdirectory inside the C<downloads_dir> parent directory.
+
+When enabled, videos will be saved inside a subdirectory specified by C<download_in_subdir_format>.
+
+=head2 download_in_subdir_format
+
+Format string used for creating the subdirectory where to download the files, when C<download_in_subdir> is enabled.
+
+The available special tokens are listed in:
+
+    pipe-viewer --tricks
+
 =head2 download_with_wget
 
 Download videos with C<wget>.
@@ -5215,7 +5254,7 @@ Token that is used to identify the user agent on the network. The agent value is
 
 =head2 video_filename_format
 
-The format of filename for downloaded files.
+Format string used for creating the filename of downloaded videos.
 
 The available special tokens are listed in: