Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use constant SHOW_TOTAL => 1;
- use Video::FFmpeg;
- foreach my $video_filename (@ARGV) {
- my $info = Video::FFmpeg::AVFormat->new($video_filename);
- print STDERR $video_filename . ': ' . $info->duration . "\n";
- open my $sub_out, '>', $video_filename . '.srt'
- or die "Cannot open file `${video_filename}.srt' for writing: $!";
- print $sub_out ×tamps($info->duration);
- }
- sub timestamps {
- my $dur = shift;
- my ($hrs, $mns, $sec) = split /:/, $dur;
- my $dur_secs = $hrs * 3600 + $mns * 60 + $sec;
- my @srt_frames;
- foreach my $curr_dur (0 .. $dur_secs) {
- push @srt_frames, sprintf
- "%d\n%s --> %s\n%s\n\n",
- # frame number (from 1)
- $curr_dur + 1,
- # beginning timestamp
- &format_duration($curr_dur, $dur_secs, 0),
- # ending timestamp
- &format_duration($curr_dur + 1, $dur_secs, 0),
- # displayed text
- &format_duration($curr_dur, $dur_secs, 1);
- }
- @srt_frames
- }
- sub format_duration {
- my ($dur, $total, $is_pretty) = @_;
- &format_ts($dur, $is_pretty) .
- (SHOW_TOTAL && $is_pretty ? ' / ' . &format_ts($total, $is_pretty) : '')
- }
- sub format_ts {
- my ($ts, $is_pretty) = @_;
- sprintf '%02d:%02d:%02d' . ($is_pretty ? '' : ',000'),
- $ts / 3600,
- $ts % 3600 / 60,
- $ts % 60
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement