Guest User

gen-timestamp-srt.pl

a guest
Jul 4th, 2010
822
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. use constant SHOW_TOTAL     => 1;
  5.  
  6. use Video::FFmpeg;
  7.  
  8. foreach my $video_filename (@ARGV) {
  9.     my $info = Video::FFmpeg::AVFormat->new($video_filename);
  10.  
  11.     print STDERR $video_filename . ': ' . $info->duration . "\n";
  12.     open my $sub_out, '>', $video_filename . '.srt'
  13.         or die "Cannot open file `${video_filename}.srt' for writing: $!";
  14.     print $sub_out &timestamps($info->duration);
  15. }
  16.  
  17. sub timestamps {
  18.     my $dur = shift;
  19.     my ($hrs, $mns, $sec) = split /:/, $dur;
  20.  
  21.     my $dur_secs = $hrs * 3600 + $mns * 60 + $sec;
  22.  
  23.     my @srt_frames;
  24.  
  25.     foreach my $curr_dur (0 .. $dur_secs) {
  26.         push @srt_frames, sprintf
  27.             "%d\n%s --> %s\n%s\n\n",
  28.             # frame number (from 1)
  29.             $curr_dur + 1,
  30.             # beginning timestamp
  31.             &format_duration($curr_dur, $dur_secs, 0),
  32.             # ending timestamp
  33.             &format_duration($curr_dur + 1, $dur_secs, 0),
  34.             # displayed text
  35.             &format_duration($curr_dur, $dur_secs, 1);
  36.     }
  37.  
  38.     @srt_frames
  39.  
  40. }
  41.  
  42. sub format_duration {
  43.     my ($dur, $total, $is_pretty) = @_;
  44.  
  45.     &format_ts($dur, $is_pretty) .
  46.     (SHOW_TOTAL && $is_pretty ? ' / ' . &format_ts($total, $is_pretty) : '')
  47. }
  48.  
  49. sub format_ts {
  50.     my ($ts, $is_pretty) = @_;
  51.     sprintf '%02d:%02d:%02d' . ($is_pretty ? '' : ',000'),
  52.         $ts / 3600,
  53.         $ts % 3600 / 60,
  54.         $ts % 60
  55. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×