2 Commits 4baa91e149 ... 8e5e296353

Author SHA1 Message Date
  trizen 8e5e296353 - CLI: added the `download_in_subdir` and `download_in_subdir_format` config-options. 3 weeks ago
  trizen b67045df92 - CLI: normalize the filename after applying the formatting. 3 weeks ago
1 changed files with 63 additions and 24 deletions
  1. 63 24
      bin/youtube-viewer

+ 63 - 24
bin/youtube-viewer

@@ -15,7 +15,7 @@
 #-------------------------------------------------------
 #-------------------------------------------------------
 #  youtube-viewer
 #  youtube-viewer
 #  Created on: 02 June 2010
 #  Created on: 02 June 2010
-#  Latest edit on: 23 August 2024
+#  Latest edit on: 25 August 2024
 #  https://github.com/trizen/youtube-viewer
 #  https://github.com/trizen/youtube-viewer
 #-------------------------------------------------------
 #-------------------------------------------------------
 
 
@@ -297,6 +297,9 @@ my %CONFIG = (
     youtube_users_file => $youtube_users_file,
     youtube_users_file => $youtube_users_file,
     remove_played_file => 0,
     remove_played_file => 0,
 
 
+    download_in_subdir        => 0,               # true to download in a subfolder
+    download_in_subdir_format => '*AUTHOR*',
+
     subscription_videos_per_channel => 20,
     subscription_videos_per_channel => 20,
 
 
     bypass_age_gate_native     => 0,
     bypass_age_gate_native     => 0,
@@ -362,7 +365,7 @@ my %CONFIG = (
     merge_with_captions => 1,
     merge_with_captions => 1,
     set_mtime           => $constant{win32} ^ 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} };
 local $SIG{__WARN__} = sub { warn @_; ++$opt{_error} };
@@ -907,7 +910,8 @@ usage: $execname [options] ([url] | [keywords])
    -d  --download!       : activate the download mode
    -d  --download!       : activate the download mode
    -dp --dl-play!        : play the video after download (with -d)
    -dp --dl-play!        : play the video after download (with -d)
    -rp --rem-played!     : delete a local video after played (with -dp)
    -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)
        --skip-if-exists! : don't download videos which already exist (with -d)
        --copy-caption!   : copy and rename the caption for downloaded videos
        --copy-caption!   : copy and rename the caption for downloaded videos
        --downloads-dir=s : downloads directory (set: '$opt{downloads_dir}')
        --downloads-dir=s : downloads directory (set: '$opt{downloads_dir}')
@@ -1896,6 +1900,7 @@ sub parse_arguments {
         'video-info!'   => \$opt{show_video_info},
         'video-info!'   => \$opt{show_video_info},
 
 
         'dp|downl-play|download-and-play|dl-play!' => \$opt{download_and_play},
         '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},
         'thousand-separator=s'           => \$opt{thousand_separator},
         'get-captions|get_captions!'     => \$opt{get_captions},
         'get-captions|get_captions!'     => \$opt{get_captions},
@@ -4127,13 +4132,15 @@ sub download_from_url {
 sub download_video {
 sub download_video {
     my ($streaming, $info) = @_;
     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 ("-")
     $video_filename =~ s/\h*:+\h*/ - /g;    # replace colons (":") with dashes ("-")
 
 
@@ -4160,23 +4167,41 @@ sub download_video {
         $audio_info->{_youtube_url} = $video_info->{_youtube_url};
         $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;
         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) {
     if ($opt{skip_if_exists} and -e $mkv_filename) {
         $video_filename = $mkv_filename;
         $video_filename = $mkv_filename;
@@ -4253,7 +4278,7 @@ sub download_video {
     # Convert the downloaded video
     # Convert the downloaded video
     if (defined $opt{convert_to}) {
     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 $convert_cmd      = $opt{convert_cmd};
 
 
         my %table = (
         my %table = (
@@ -4297,7 +4322,7 @@ sub download_video {
         # Remove it afterwards
         # Remove it afterwards
         if ($? == 0 and $opt{remove_played_file}) {
         if ($? == 0 and $opt{remove_played_file}) {
             unlink $video_filename
             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";
         }
         }
     }
     }
 
 
@@ -5476,6 +5501,20 @@ By enabling this option and setting the value of C<ytdl_cmd> to C<"yt-dlp">, dow
 
 
 Directory where to download files and where to save converted files.
 Directory where to download files and where to save converted files.
 
 
+=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 env_proxy
 =head2 env_proxy
 
 
 Load proxy settings from C<*_proxy> environment variables (if any).
 Load proxy settings from C<*_proxy> environment variables (if any).
@@ -5713,7 +5752,7 @@ Token that is used to identify the user agent on the network. The agent value is
 
 
 =head2 video_filename_format
 =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:
 The available special tokens are listed in: