AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
avProcessor.cpp
Go to the documentation of this file.
4 
5 #include <iostream>
6 #include <iomanip>
7 #include <vector>
8 #include <fstream>
9 #include <sstream>
10 #include <cstdlib>
11 
12 static const size_t dummyWidth = 1920;
13 static const size_t dummyHeight = 1080;
14 static const std::string dummyPixelFormat = "yuv420p";
15 static const std::string dummyVideoCodec = "mpeg2video";
16 
17 static const size_t dummySampleRate = 48000;
18 static const size_t dummyChannels = 1;
19 static const std::string dummySampleFormat = "s16";
20 static const std::string dummyAudioCodec = "pcm_s16le";
21 
22 static bool useVideoGenerator = false;
23 
24 void parseConfigFile(const std::string& configFilename, avtranscoder::Transcoder& transcoder)
25 {
26  std::ifstream configFile(configFilename.c_str(), std::ifstream::in);
27 
28  std::string line;
29  while(std::getline(configFile, line))
30  {
31  std::istringstream is_line(line);
32  std::string filename;
33  if(std::getline(is_line, filename, '='))
34  {
35  std::string streamId;
36  if(std::getline(is_line, streamId, ':'))
37  {
38  std::string transcodeProfile;
39  std::getline(is_line, transcodeProfile);
40 
41  std::stringstream ss(streamId);
42  size_t streamIndex = 0;
43  char separator;
44  int subStreamIndex = -1;
45  ss >> streamIndex;
46  ss >> separator;
47  if(separator == '.')
48  ss >> subStreamIndex;
49 
50  // dummy stream, need a ICodec (audio or video)
51  if(!filename.length())
52  {
54  {
55  // video
58  inputCodec.setImageParameters(imageDesc);
59 
60  transcoder.add(filename, streamIndex, subStreamIndex, transcodeProfile, inputCodec);
61  }
62  else
63  {
64  // audio
67  inputCodec.setAudioParameters(audioDesc);
68 
69  transcoder.add(filename, streamIndex, subStreamIndex, transcodeProfile, inputCodec);
70  }
71  }
72  else
73  {
74  transcoder.add(filename, streamIndex, subStreamIndex, transcodeProfile);
75  }
76  }
77  }
78  }
79 
80  configFile.close();
81 }
82 
83 int main(int argc, char** argv)
84 {
85  std::string help;
86  help += "Usage\n";
87  help += "\tavprocessor CONFIG.TXT OUTPUT_FILE_NAME [--generate-black] [--verbose] [--logFile] [--help]\n";
88  help += "CONFIG.TXT\n";
89  help += "\tEach line will be one stream in the output.\n";
90  help += "\tPattern of each line is:\n";
91  help += "\t[inputFile]=STREAM_ID.[subStreamId]:[profileName]\n";
92  help += "\tNo inputFile: will generate black image / audio silence (audio by default)\n";
93  help += "\tNo subStreamId: will process of channels of the stream\n";
94  help += "\tNo profileName: will rewrap the stream\n";
95  help += "Command line options\n";
96  help += "\t--generate-black: stream which not referred to an input, will generate an output video stream with black "
97  "images (by default generate audio stream with silence)\n";
98  help += "\t--verbose: set log level to AV_LOG_DEBUG\n";
99  help += "\t--logFile: put log in 'avtranscoder.log' file\n";
100  help += "\t--help: display this help\n";
101 
102  // Preload FFmpeg context
104  avtranscoder::Logger::setLogLevel(AV_LOG_QUIET);
105 
106  // List command line arguments
107  std::vector<std::string> arguments;
108  for(int argument = 1; argument < argc; ++argument)
109  {
110  arguments.push_back(argv[argument]);
111  }
112  for(size_t argument = 0; argument < arguments.size(); ++argument)
113  {
114  if(arguments.at(argument) == "--help")
115  {
116  std::cout << help << std::endl;
117  return 0;
118  }
119  else if(arguments.at(argument) == "--generate-black")
120  {
121  useVideoGenerator = true;
122  }
123  else if(arguments.at(argument) == "--verbose")
124  {
125  avtranscoder::Logger::setLogLevel(AV_LOG_DEBUG);
126  }
127  else if(arguments.at(argument) == "--logFile")
128  {
130  }
131  }
132 
133  // Check required arguments
134  if(argc < 3)
135  {
136  std::cout << "avprocessor can rewrap or transcode inputs to create an output media file." << std::endl;
137  std::cout << "Use option --help to display help" << std::endl;
138  return (-1);
139  }
140 
141  try
142  {
143  std::string inputConfigFile(argv[1]);
144  avtranscoder::OutputFile outputFile(argv[2]);
145 
146  avtranscoder::Transcoder transcoder(outputFile);
148 
149  parseConfigFile(inputConfigFile, transcoder);
150 
152  transcoder.process(progress);
153  }
154  catch(std::exception& e)
155  {
156  std::cerr << "ERROR: during process, an error occured: " << e.what() << std::endl;
157  }
158  catch(...)
159  {
160  std::cerr << "ERROR: during process, an unknown error occured" << std::endl;
161  }
162 }
static const size_t dummyWidth
Definition: avProcessor.cpp:12
static const std::string dummySampleFormat
Definition: avProcessor.cpp:19
Description to create a video frame.
Definition: VideoFrame.hpp:21
static const std::string dummyPixelFormat
Definition: avProcessor.cpp:14
Outputfile is the default implentation of wrapper which uses LibAV/FFMpeg.
Definition: OutputFile.hpp:17
void setImageParameters(const VideoFrameDesc &videoFrameDesc)
Definition: VideoCodec.cpp:34
static const std::string dummyVideoCodec
Definition: avProcessor.cpp:15
const std::string separator
Definition: util.hpp:33
A Transcoder manages a list of streams, and process a transcode to create an output media file...
Definition: Transcoder.hpp:42
static const size_t dummySampleRate
Definition: avProcessor.cpp:17
Implementation of IProgress, to display a progress bar in console.
static bool useVideoGenerator
Definition: avProcessor.cpp:22
void preloadCodecsAndFormats()
Register all the codecs and formats which are enabled at configuration time.
Definition: common.cpp:16
void parseConfigFile(const std::string &configFilename, avtranscoder::Transcoder &transcoder)
Definition: avProcessor.cpp:24
void add(const std::string &filename)
Add all streams of the file with the given filename. All the streams will be rewrapped.
Definition: Transcoder.cpp:38
static const std::string dummyAudioCodec
Definition: avProcessor.cpp:20
Description to create an audio frame. This corresponds to the number of samples, which corresponds to...
Definition: AudioFrame.hpp:14
static const size_t dummyChannels
Definition: avProcessor.cpp:18
static void logInFile()
Log ffmpeg/libav and avtranscoder informations in a text file.
Definition: log.cpp:62
void setProcessMethod(const EProcessMethod eProcessMethod, const size_t indexBasedStream=0, const double outputDuration=0)
Set the transcoding policy.
Definition: Transcoder.cpp:326
void setAudioParameters(const AudioFrameDesc &audioFrameDesc)
Definition: AudioCodec.cpp:30
int main(int argc, char **argv)
Definition: avProcessor.cpp:83
ProcessStat process(IProgress &progress)
Process all the streams, and ended the process depending on the transcode politic.
Definition: Transcoder.cpp:272
static const size_t dummyHeight
Definition: avProcessor.cpp:13
static void setLogLevel(const int level)
Set the log level of ffmpeg/libav.
Definition: log.cpp:26