AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StreamProperties.cpp
Go to the documentation of this file.
1 #include "StreamProperties.hpp"
2 
5 
6 #include <stdexcept>
7 
8 namespace avtranscoder
9 {
10 
11 StreamProperties::StreamProperties(const FormatContext& formatContext, const size_t index)
12  : _formatContext(&formatContext.getAVFormatContext())
13  , _codecContext(NULL)
14  , _codec(NULL)
15  , _streamIndex(index)
16 {
17  if(_formatContext)
18  detail::fillMetadataDictionnary(_formatContext->streams[index]->metadata, _metadatas);
19 
20  if(_formatContext)
21  {
22  if(_streamIndex > _formatContext->nb_streams)
23  {
24  std::stringstream ss;
25  ss << "Stream at index " << _streamIndex << " does not exist.";
26  throw std::runtime_error(ss.str());
27  }
28  _codecContext = _formatContext->streams[_streamIndex]->codec;
29  }
30 
32  _codec = avcodec_find_decoder(_codecContext->codec_id);
33 }
34 
36 {
37 }
38 
40 {
41  if(!_formatContext)
42  throw std::runtime_error("unknown format context");
43  return _formatContext->streams[_streamIndex]->id;
44 }
45 
47 {
48  if(!_formatContext)
49  throw std::runtime_error("unknown format context");
50  return _formatContext->streams[_streamIndex]->time_base;
51 }
52 
54 {
55  const Rational timeBase = getTimeBase();
56  return av_q2d(timeBase) * _formatContext->streams[_streamIndex]->duration;
57 }
58 
60 {
61  if(!_formatContext)
62  throw std::runtime_error("unknown format context");
63  return _formatContext->streams[_streamIndex]->codec->codec_type;
64 }
65 
67 {
68  if(!_codecContext)
69  throw std::runtime_error("unknown codec context");
70  return _codecContext->codec_id;
71 }
72 
73 std::string StreamProperties::getCodecName() const
74 {
75  if(!_codecContext || !_codec)
76  throw std::runtime_error("unknown codec");
77 
78  if(_codec->capabilities & CODEC_CAP_TRUNCATED)
79  _codecContext->flags |= CODEC_FLAG_TRUNCATED;
80 
81  if(!_codec->name)
82  throw std::runtime_error("unknown codec name");
83 
84  return std::string(_codec->name);
85 }
86 
88 {
89  if(!_codecContext || !_codec)
90  throw std::runtime_error("unknown codec");
91 
92  if(_codec->capabilities & CODEC_CAP_TRUNCATED)
93  _codecContext->flags |= CODEC_FLAG_TRUNCATED;
94 
95  if(!_codec->long_name)
96  throw std::runtime_error("unknown codec long name");
97 
98  return std::string(_codec->long_name);
99 }
100 
102 {
103  PropertyVector propertyVector;
104  return fillVector(propertyVector);
105 }
106 
108 {
109  addProperty(data, "streamId", &StreamProperties::getStreamId);
110  addProperty(data, "streamIndex", &StreamProperties::getStreamIndex);
111  addProperty(data, "timeBase", &StreamProperties::getTimeBase);
112  addProperty(data, "duration", &StreamProperties::getDuration);
113  addProperty(data, "codecId", &StreamProperties::getCodecId);
114  addProperty(data, "codecName", &StreamProperties::getCodecName);
115  addProperty(data, "codecLongName", &StreamProperties::getCodecLongName);
116 
117  for(size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex)
118  {
119  detail::add(data, _metadatas.at(metadataIndex).first, _metadatas.at(metadataIndex).second);
120  }
121 
122  return data;
123 }
124 
126 {
127  PropertyMap dataMap;
128 
129  PropertyVector dataVector(asVector());
130  for(PropertyVector::const_iterator it = dataVector.begin(); it != dataVector.end(); ++it)
131  {
132  dataMap.insert(std::make_pair(it->first, it->second));
133  }
134 
135  return dataMap;
136 }
137 
138 std::string StreamProperties::asJson() const
139 {
141  PropertyMap properties = asMap();
142  for(PropertyMap::iterator it = properties.begin(); it != properties.end(); ++it)
143  writer << std::make_pair(it->first.c_str(), it->second.c_str());
144  return writer.build();
145 }
146 
147 std::ostream& operator<<(std::ostream& flux, const StreamProperties& streamProperties)
148 {
149  flux << std::left;
150  flux << detail::separator << " Stream " << detail::separator << std::endl;
151 
152  PropertyVector properties = streamProperties.asVector();
153  for(PropertyVector::iterator it = properties.begin(); it != properties.end(); ++it)
154  {
155  flux << std::setw(detail::keyWidth) << it->first << ": " << it->second << std::endl;
156  }
157 
158  return flux;
159 }
160 }
std::vector< std::pair< std::string, std::string > > PropertyVector
PropertyVector is a vector of pair, because the order of properties matters to us.
Definition: util.hpp:23
const AVFormatContext * _formatContext
Has link (no ownership)
AVMediaType getStreamType() const
const size_t keyWidth
Definition: util.hpp:32
std::string getCodecName() const
const std::string separator
Definition: util.hpp:33
void add(PropertyVector &propertyVector, const std::string &key, const size_t &value)
Definition: util.cpp:16
AVCodec * _codec
Has link (no ownership)
PropertyMap asMap() const
Return all properties as a map (name of property, value)
void fillMetadataDictionnary(AVDictionary *avdictionnary, PropertyVector &metadata)
Fill metadata parameter with the given AVDictionary.
Definition: util.cpp:55
float getDuration() const
in seconds
std::map< std::string, std::string > PropertyMap
Definition: util.hpp:24
std::string asJson() const
Return all properties as a json format.
Wrapper of an AVFormatContext.
virtual PropertyVector & fillVector(PropertyVector &data) const
To avoid copy of the vector.
std::string getCodecLongName() const
AVCodecContext * _codecContext
Has link (no ownership)
Virtual based class of properties for all types of stream.
void addProperty(PropertyVector &dataVector, const std::string &key, T(StreamProperties::*getter)(void) const) const
AVRational Rational
Definition: common.hpp:55
StreamProperties(const FormatContext &formatContext, const size_t index)
Write an object to a stream.
Definition: JsonWriter.hpp:84
std::ostream & operator<<(std::ostream &flux, const InputFile &input)
Definition: InputFile.cpp:171
PropertyVector asVector() const
Same data with a specific order.