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