#!/usr/bin/env perl
#
=head1 NAME

B<fftrim> - concatenate, trim and compress video files

=head1 VERSION

Version 0.01

=cut

package App::fftrim;
use 5.006;
use strict;
use warnings;
use App::fftrim;
use Path::Tiny;
use autodie ':all';
use feature 'say';
use Cwd;
use Getopt::Long::Descriptive;
our ($opt, $usage) = describe_options(
   '%c %o',
   [ 'in=s',			"input file(s)"],
   [ 'out=s',			"output file"],
   [ 'start=s',			"start time"],
   [ 'end=s',			"end time"],
   [ 'concat-only|c',	"concatenate only" ],
   [ 'profile|p=s',       'merge ffmpeg options from named file in $HOME/.fftrim'],
   [ 'source-dir=s',  	"batch mode, source directory" ],
   [ 'target-dir=s',	"batch mode, destination directory" ],
   [ 'frame-rate|r=s',  "specify frame rate" ],
   [ 'auto-frame-rate|a',"use same frame rate as source" ],
   [ 'n',      			"simulate: print commands but do not run them" ],
   [ 'm',      			"simulate: print commands omitting file checks" ],
   [ 'old-concat',		"old naming for intermediate file" ],
   [ 'help',   			"print usage message and exit" ],
 );
initial_setup() and exit;
print($usage->text, 
	"\nSingle-file options --in, --out, --start and --end cannot
be used with batch mode options --source-dir and --target-dir.\n\n"),
	exit if $opt->{help} or ! keys %$opt;

process_args();


__DATA__
# change the following ffmpeg options as suits

-c:v libx264
-preset:v medium
-profile:v main
-tune:v film
-s:v 640x360
-c:a aac
-b:a 80k
-ac 2
-ar 24000
-level 3

# don't change these arguments
-strict -2

=head1 SYNOPSIS

C<fftrim --in 00001.MTS --out clip1.mp4 --start 15.5 --end 44:13>

C<fftrim --in "00001.MTS 00002.MTS" --out clip2.mp4 --start 44:13 --end 2-24:55>

=head1 DESCRIPTION

B<fftrim> processes raw videos from a camcorder by
concatenating, trimming and compressing them according to
arguments you supply on the command line, or in a F<CONTROL>
file.

Concatenation is performed using ffmpeg's "concat protocol".
This file-level concatenation is suitable for MTS files
generated by a Sony or Pansonic camera. 

=head1 Trimming a single file

C<fftrim --in 00001.MTS --out clip1.mp4 --start 15.5 --end 44:13>

=head1 Concatenating multiple source files

C<fftrim --in "00001.MTS 00002.MTS 00003.MTS" --out clip2.mp4 --start 44:13 --end 2-24:55>

C<fftrim --in "00001.MTS 00002.MTS 00003.MTS" --out clip3.mp4 --start 2-24:55 --end 3-1:05>

The expression C<2-24:55> means a position in the concatenated
file that includes the full length of the first clip and
24:55 of the second clip. Similarly, C<3-65> or
C<3-1:05> would mean a position of 1:05 into the third clip.

The source files are concatenated into an intermediate file
using the name of the first source file appended with .mp4.

=head1 Batch mode

C<fftrim --source-dir raw --target-dir final>

It's probably better to try with the -n or -m flag first.

C<fftrim -n --source-dir raw --target-dir final>

There is also some simple error checking, and fftrim
will abort if errors are found in the CONTROL file.

=head1 CONTROL file format

The CONTROL file used for batch processing
appears in the same directory as the source
video files. It contains multiple lines
of the following format:

    # source file(s)    output file   start  end
    # ---------------   -----------   -----  ----
      00001.MTS       : part1.mp4   : 15.5 : 44:13 

Arguments are separated by a colon character flanked by
whitespace. Commented lines are ignored.

The following line creates F<part2.mp4> from source files
F<00001.MTS> and F<00002.MTS>:

    00001.MTS 00002.MTS : part2.mp4 :  44:13 : 2-24:55 

=head1 Profiles

F<$HOME/.fftrim> is the directory for profiles, 
which are text files containing ffmpeg options. 

Adding C<--profile highres> to the command line will
merge options from the file F<$HOME/.fftrim/highres>.

Be sure to look at F<$HOME/.fftrim/default>. These options are
are merged by default, that is when C<--profile> is not supplied.
Please change them to suit yourself.


=head1 AUTHOR

Joel Roth, C<< <joelz at pobox.com > >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-app-fftrim at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-fftrim>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc fftrim

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=App-fftrim>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/App-fftrim>

=item * CPAN Ratings

L<https://cpanratings.perl.org/d/App-fftrim>

=item * Search CPAN

L<https://metacpan.org/release/App-fftrim>

=back

=head1 ACKNOWLEDGEMENTS

Thanks to ffmpeg developers and the community.

=head1 LICENSE AND COPYRIGHT

Copyright 2018 Joel Roth.

This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:

L<http://www.perlfoundation.org/artistic_license_2_0>

Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


=cut

