Update GPL boilerplate and (C)
[ardour.git] / gtk2_ardour / transcode_ffmpeg.h
1 /*
2  * Copyright (C) 2013-2018 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #ifndef __ardour_transcode_ffmpeg_h__
19 #define __ardour_transcode_ffmpeg_h__
20
21 #include <string>
22 #include "ardour/system_exec.h"
23 #include "ardour/types.h"
24
25
26 /** @class TranscodeFfmpeg
27  *  @brief wrapper around ffmpeg and ffprobe command-line utils
28  *
29  *  This class includes parsers for stdi/o communication with
30  *  'ffmpeg' and 'ffprobe' and provide an abstraction to
31  *  transcode video-files and extract aufio tracks and query
32  *  file information.
33  */
34 class TranscodeFfmpeg : public sigc::trackable
35                       , public PBD::ScopedConnectionList
36 {
37         public:
38
39         struct FFAudioStream {
40                 std::string name;
41                 std::string stream_id;
42                 uint32_t channels;
43         };
44         typedef std::vector<FFAudioStream> FFAudioStreams;
45         typedef std::map<std::string,std::string> FFSettings;
46
47
48                 /** instantiate a new transcoder. If a file-name is given, the file's
49                  * attributes (fps, duration, geometry etc) are read.
50                  *
51                  * @param f path to the video-file to probe or use as input for
52                  * \ref extract_audio and \ref transcode.
53                  */
54                 TranscodeFfmpeg (std::string f);
55                 virtual ~TranscodeFfmpeg ();
56                 /** transcode/import a video-file
57                  * @param outfile full-path (incl. file-extension)
58                  * @param outwidth video-width, \c <0 no scaling)
59                  * @param outheight video-height \c <0 use aspect \c \ref outwidth /c / \c aspect-ratio
60                  * @param kbitps video bitrate \c 0 calculate to use 0.7 bits per pixel on average
61                  * @return \c true if the transcoder process was successfully started.
62                  */
63                 bool transcode (std::string, const int outwidth=0, const int outheight=0, const int kbitps =0);
64                 /** Extract an audio track from the given input file to a new 32bit float little-endian PCM WAV file.
65                  * @param outfile full-path (incl. file-extension) to .wav file to write
66                  * @param samplerate target samplerate
67                  * @param stream Stream-ID of the audio-track to extract
68                  * specified as element-number in \ref get_audio().
69                  * @return \c true if the transcoder process was successfully started.
70                  */
71                 bool extract_audio (std::string outfile, ARDOUR::samplecnt_t samplerate, unsigned int stream=0);
72                 /** transcode video and mux audio files into a new video-file.
73                  * @param outfile full-path of output file to create (existing files are overwritten)
74                  * @param inf_a filename of input audio-file
75                  * @param inf_v filename of input video-file
76                  * @param ffs additional command-line parameters for 'ffmpeg'. key/value pairs
77                  * eg ffs["-vcodec"] = "mpeg4"
78                  * @param meta additional meta-data results in -metadata "<key>"="<value>" command-line
79                  * arguments
80                  * @param map if set to \c true stream mapping from input streams to output streams is set to use
81                  * only the first available stream from the audio & video file (-map 0.0 -map 1.0).
82                  * @return \c true if the encoder process was successfully started.
83                  */
84                 bool encode (std::string outfile, std::string inf_a, std::string inf_v, FFSettings ffs, FFSettings meta, bool map = true);
85                 /** @return array with default encoder settings */
86                 FFSettings default_encoder_settings ();
87                 /** @return array with default meta data */
88                 FFSettings default_meta_data ();
89                 /** abort any running transcoding process */
90                 void cancel();
91                 /**
92                  * @return \c true if the input file was parsed correctly on class creation. */
93                 bool probe_ok () { return probeok; }
94                 /** @return \c true if the ffmpeg/ffparse executables are avail on this system */
95                 bool ffexec_ok () { return ffexecok; }
96
97                 /** signal emitted when ffmpeg reports progress updates
98                  * during \ref encode \ref transcode and \ref extract_audio
99                  * The parameters are current and last video-frame.
100                  */
101                 PBD::Signal2<void, ARDOUR::samplecnt_t, ARDOUR::samplecnt_t> Progress;
102                 /** signal emitted when the transcoder process terminates. */
103                 PBD::Signal0<void> Finished;
104
105                 double get_fps () { return m_fps; }
106                 double get_aspect () { return m_aspect; }
107                 int    get_width() { return m_width; }
108                 int    get_height() { return m_height; }
109                 ARDOUR::samplecnt_t get_duration() { return m_duration; }
110                 std::string  get_codec() { return m_codec; }
111
112                 FFAudioStreams get_audio() { return m_audio; }
113
114                 /** override file duration used with the \ref Progress signal.
115                  * @param d duration in video-frames = length_in_seconds * get_fps()
116                  */
117                 void set_duration(ARDOUR::samplecnt_t d) { m_duration = d; }
118
119                 /* offset, lead-in/out are in seconds */
120                 void set_avoffset(double av_offset) { m_avoffset = av_offset; }
121                 void set_leadinout(double lead_in, double lead_out) { m_lead_in = lead_in; m_lead_out = lead_out; }
122
123                 void set_fps(double fps) { m_fps = fps; } // on export, used for rounding only.
124
125 #if 1 /* tentative debug mode */
126                 void   set_debug (bool onoff) { debug_enable = onoff; }
127 #endif
128         protected:
129                 std::string infile;
130                 ARDOUR::SystemExec  *ffcmd;
131
132                 bool probe ();
133
134                 double m_fps;
135                 double m_aspect;
136                 std::string m_sar;
137                 ARDOUR::samplecnt_t m_duration;
138                 int m_width;
139                 int m_height;
140                 std::string m_codec;
141
142                 int m_videoidx;
143                 double m_avoffset;
144                 double m_lead_in;
145                 double m_lead_out;
146                 bool ffexecok;
147                 bool probeok;
148
149                 FFAudioStreams m_audio;
150
151                 void ffmpegparse_v (std::string d, size_t s);
152                 void ffmpegparse_a (std::string d, size_t s);
153                 void ffprobeparse (std::string d, size_t s);
154                 void ffexit ();
155                 std::string ffoutput;
156
157                 std::string ffmpeg_exe;
158                 std::string ffprobe_exe;
159 #if 1 /* tentative debug mode */
160                 bool debug_enable;
161 #endif
162 };
163
164 #endif /* __ardour_transcode_ffmpeg_h__ */