Initial import from SVN

This commit is contained in:
emeric
2014-03-09 10:58:52 +01:00
commit b6abce0c2f
440 changed files with 30862 additions and 0 deletions
+187
View File
@@ -0,0 +1,187 @@
#include <iostream>
#include <sstream>
#include <boost/iostreams/stream.hpp>
#include <boost/process.hpp>
#include <boost/foreach.hpp>
#include "AvConvTranscoder.hpp"
namespace Transcode
{
boost::filesystem::path AvConvTranscoder::_avConvPath = "";
void
AvConvTranscoder::init()
{
_avConvPath = boost::process::search_path("avconv");
std::cout << "Using execPath " << _avConvPath << std::endl;
}
//boost::filesystem::path AvConvTranscoder::_avConvPath = "";
AvConvTranscoder::AvConvTranscoder(const Parameters& parameters)
: _parameters(parameters),
_outputPipe(boost::process::create_pipe()),
_source(_outputPipe.source, boost::iostreams::close_handle),
_is(_source),
_in(&_is),
_isComplete(false)
{
if (!boost::filesystem::exists(_parameters.getInputMediaFile().getPath())) {
throw std::runtime_error("File " + _parameters.getInputMediaFile().getPath().string() + " does not exists!");
}
else if (!boost::filesystem::is_regular( _parameters.getInputMediaFile().getPath())) {
throw std::runtime_error("File " + _parameters.getInputMediaFile().getPath().string() + " is not regular!");
}
std::cout << "Transcoding file '" << _parameters.getInputMediaFile().getPath() << "'" << std::endl;
// Launch a process to handle the conversion
boost::iostreams::file_descriptor_sink sink(_outputPipe.sink, boost::iostreams::close_handle);
std::ostringstream oss;
oss << "avconv";
// input Offset
oss << " -ss " << _parameters.getOffset().total_seconds(); // to be placed before '-i' to speed up seeking
// Input file
oss << " -i \"" << _parameters.getInputMediaFile().getPath().string() << "\"";
// Output bitrates
oss << " -b:a " << _parameters.getOutputAudioBitrate() ;
if (_parameters.getOutputFormat().getType() == Format::Video)
oss << " -b:v " << _parameters.getOutputVideoBitrate();
// Stream mapping
{
typedef std::map<Stream::Type, Stream::Id> StreamMap;
const StreamMap& streamMap = _parameters.getInputStreams();
BOOST_FOREACH(const StreamMap::value_type& inputStream, streamMap)
{
// HACK (no subtitle support yet)
if (inputStream.first != Stream::Subtitle)
oss << " -map 0:" << inputStream.second; // 0 means input file index
}
}
// Codecs and formats
switch( _parameters.getOutputFormat().getEncoding())
{
case Format::MP3:
oss << " -f mp3";
break;
case Format::OGA:
oss << " -acodec libvorbis -f ogg";
break;
case Format::OGV:
oss << " -acodec libvorbis -ac 2 -ar 44100 -vcodec libtheora -threads 4 -f ogg";
break;
case Format::WEBMA:
oss << " -codec:a libvorbis -f webm";
break;
case Format::WEBMV:
oss << " -acodec libvorbis -ac 2 -ar 44100 -vcodec libvpx -threads 4 -f webm";
break;
case Format::M4A:
oss << " -acodec aac -f mp4";
break;
case Format::M4V:
oss << " -acodec aac -strict experimental -ac 2 -ar 44100 -vcodec libx264 -f m4v";
break;
case Format::FLV:
oss << " -acodec libmp3lame -ac 2 -ar 44100 -vcodec libx264 -f flv";
break;
default:
assert(0);
}
oss << " -"; // output to stdout
std::cout << "executing... '" << oss.str() << "'" << std::endl;
std::vector<int> ranges = { 3, 1024 }; // fd range to be closed
_child = std::make_shared<boost::process::child>( boost::process::execute(
boost::process::initializers::run_exe(_avConvPath),
boost::process::initializers::set_cmd_line(oss.str()),
boost::process::initializers::bind_stdout(sink),
boost::process::initializers::close_fd(STDIN_FILENO),
boost::process::initializers::close_fds(ranges)
)
);
}
void
AvConvTranscoder::process(void)
{
std::size_t readDatasSize = 1024; // TODO parametrize elsewhere?
char ch;
while(readDatasSize != 0 && _in && _in.get(ch)) {
_data.push_back(ch);
--readDatasSize;
}
if (!_in || _in.fail() || _in.eof()) {
std::cout << "Transcode complete!" << std::endl;
waitChild();
_isComplete = true;
}
}
AvConvTranscoder::~AvConvTranscoder()
{
std::cout << "AvConvTranscoder::~AvConvTranscoder called!" << std::endl;
if (_in.eof())
waitChild();
else
killChild();
}
void
AvConvTranscoder::waitChild()
{
try {
if (_child) {
std::cout << "waiting for child!" << std::endl;
boost::process::wait_for_exit(*_child);
std::cout << "waiting for child! DONE" << std::endl;
_child.reset();
}
}
catch( std::exception& e)
{
std::cerr << "Exception caugh in waitChild: " << e.what() << std::endl;
}
}
void
AvConvTranscoder::killChild()
{
try {
if (_child) {
std::cout << "Killing child!" << std::endl;
boost::process::terminate(*_child);
std::cout << "Killing child DONE" << std::endl;
_child.reset();
}
}
catch( std::exception& e)
{
std::cerr << "Exception caugh in killChild: " << e.what() << std::endl;
}
}
} // namespace Transcode
+62
View File
@@ -0,0 +1,62 @@
#ifndef AVCONV_TRANSCODER_HPP
#define AVCONV_TRANSCODER_HPP
#include <memory>
#include <iostream>
#include <deque>
#include <boost/iostreams/stream.hpp>
#include <boost/process.hpp>
#include <boost/filesystem.hpp>
#include "Parameters.hpp"
namespace Transcode
{
class AvConvTranscoder
{
public:
typedef std::deque<unsigned char> data_type;
static void init();
~AvConvTranscoder();
AvConvTranscoder(const Parameters& parameters);
data_type& getOutputData() { return _data; }
const Parameters& getParameters(void) const { return _parameters; }
// Process a bunch of input data
void process(void);
bool isComplete(void) const { return _isComplete;};
private:
typedef std::shared_ptr<boost::process::child> ChildPtr;
void waitChild();
void killChild();
const Parameters _parameters;
boost::process::pipe _outputPipe;
boost::iostreams::file_descriptor_source _source;
boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_source> _is;
std::istream _in;
std::shared_ptr<boost::process::child> _child;
static boost::filesystem::path _avConvPath;
data_type _data;
bool _isComplete;
};
} // naspace Transcode
#endif
+64
View File
@@ -0,0 +1,64 @@
#include <boost/foreach.hpp>
#include <stdexcept>
#include <cassert>
#include "Format.hpp"
namespace Transcode
{
const std::vector<Format> Format::_supportedFormats
{
{Format::OGA, Format::Audio, "audio/ogg", "Ogg"},
{Format::OGV, Format::Video, "video/ogg", "Ogg"},
{Format::MP3, Format::Audio, "audio/mp3", "MP3"},
{Format::WEBMA, Format::Audio, "audio/webm", "WebM"},
{Format::WEBMV, Format::Video, "video/webm", "WebM"},
{Format::FLV, Format::Video, "video/x-flv", "Flash Video"},
{Format::M4A, Format::Audio, "audio/mp4", "MP4"},
{Format::M4V, Format::Video, "video/mp4", "MP4"},
};
Format::Format(Encoding encoding, Type type, std::string mimeType, std::string desc)
:
_encoding(encoding),
_type(type),
_mineType(mimeType),
_desc(desc)
{}
const Format&
Format::get(Encoding encoding)
{
BOOST_FOREACH(const Format& format, _supportedFormats)
{
if (format.getEncoding() == encoding)
return format;
}
throw std::runtime_error("Cannot find format");
}
std::vector<Format>
Format::get(Type type)
{
std::vector<Format> res;
BOOST_FOREACH(const Format& format, _supportedFormats)
{
if (format.getType() == type)
res.push_back( format );
}
return res;
}
bool
Format::operator==(const Format& other) const
{
return getEncoding() == other.getEncoding();
}
} // namespace Transcode
+57
View File
@@ -0,0 +1,57 @@
#ifndef TRANSCODE_FORMAT_HPP
#define TRANSCODE_FORMAT_HPP
#include <vector>
#include <string>
namespace Transcode
{
class Format
{
public:
// Output format
enum Encoding {
OGA,
OGV,
MP3,
WEBMA,
WEBMV,
FLV,
M4A,
M4V,
};
enum Type {
Video,
Audio,
};
// Utility
static const Format& get(Encoding encoding);
static std::vector<Format> get(Type type);
Format(Encoding format, Type type, std::string mimeType, std::string desc);
Encoding getEncoding(void) const { return _encoding;}
Type getType(void) const { return _type;}
const std::string& getMimeType(void) const { return _mineType;}
const std::string& getDesc(void) const { return _desc;}
bool operator==(const Format& other) const;
private:
Encoding _encoding;
Type _type;
std::string _mineType;
std::string _desc;
static const std::vector<Format> _supportedFormats;
};
} // namespace Transcode
#endif
+87
View File
@@ -0,0 +1,87 @@
#include <list>
#include <boost/foreach.hpp>
#include "InputMediaFile.hpp"
#include "av/InputFormatContext.hpp"
namespace Transcode
{
bool getStreamType(enum AVMediaType type, Transcode::Stream::Type& streamType)
{
switch (type) {
case AVMEDIA_TYPE_VIDEO: streamType = Transcode::Stream::Video; return true;
case AVMEDIA_TYPE_AUDIO: streamType = Transcode::Stream::Audio; return true;
case AVMEDIA_TYPE_SUBTITLE: streamType = Transcode::Stream::Subtitle; return true;
default:
return false;
}
}
InputMediaFile::InputMediaFile(const boost::filesystem::path& p)
: _path(p)
{
Av::InputFormatContext input(_path);
input.findStreamInfo();
// Calculate estimated duration
if (input.getDurationSecs())
_duration = boost::posix_time::seconds(input.getDurationSecs() + 1);
// Get input streams
std::vector<Av::Stream> streams = input.getStreams();
std::list<enum AVMediaType> types;
for (std::size_t streamId = 0; streamId < streams.size(); ++streamId)
{
Stream::Type type;
if (getStreamType(streams[streamId].getCodecContext().getType(), type))
{
types.push_back(streams[streamId].getCodecContext().getType());
_streams.push_back( Stream(streamId,
type,
streams[streamId].getMetadata().get("language"), // TODO define somewhere else?
streams[streamId].getCodecContext().getCodecDesc()
));
}
}
types.unique();
// Scan for best streams
BOOST_FOREACH(enum AVMediaType type, types)
{
Av::Stream::Idx index;
if (input.getBestStreamIdx(type, index))
{
Stream::Type streamType;
if (getStreamType(type, streamType))
_bestStreams.insert(std::make_pair( streamType, index) );
}
else
std::cerr << "Cannot find best stream for type " << type << std::endl;
}
}
std::vector<Stream>
InputMediaFile::getStreams(Stream::Type type) const
{
std::vector<Stream> res;
BOOST_FOREACH(const Stream& stream, _streams)
{
if (stream.getType() == type)
res.push_back(stream);
}
return res;
}
} // namespace Transcode
+52
View File
@@ -0,0 +1,52 @@
#ifndef TRANSCODE_INPUT_MEDIA_FILE
#define TRANSCODE_INPUT_MEDIA_FILE
#include <map>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "Stream.hpp"
namespace Transcode
{
class InputMediaFile
{
public:
enum StreamType
{
StreamAudio,
StreamVideo,
StreamSubtitle,
};
InputMediaFile(const boost::filesystem::path& p);
// Accessors
boost::filesystem::path getPath(void) const {return _path;}
boost::posix_time::time_duration getDuration(void) const {return _duration;}
// Stream handling
std::vector<Stream> getStreams(Stream::Type type) const;
const std::map<Stream::Type, Stream::Id>& getBestStreams(void) const {return _bestStreams;}
private:
boost::filesystem::path _path;
boost::posix_time::time_duration _duration;
std::vector<Stream> _streams;
std::map<Stream::Type, Stream::Id> _bestStreams;
};
} // namespace Transcode
#endif // TRANSCODE_INPUT_MEDIA_FILE
+43
View File
@@ -0,0 +1,43 @@
#include "av/InputFormatContext.hpp"
#include "Parameters.hpp"
namespace Transcode
{
std::string
getMimeType(Format format)
{
//TODO
return "";
}
Parameters::Parameters(const InputMediaFile& inputMediaFile,
const Format& outputFormat,
std::size_t audioBitrate)
:
_mediaFile(inputMediaFile),
_outputFormat(outputFormat),
_outputAudioBitrate(audioBitrate),
_outputVideoBitrate(0)
{
// By default, select the best stream indexes
_inputStreams = _mediaFile.getBestStreams();
}
Parameters::Parameters(const InputMediaFile& inputMediaFile,
const Format& outputFormat,
std::size_t audioBitrate,
std::size_t videoBitrate)
:
_mediaFile(inputMediaFile),
_outputFormat(outputFormat),
_outputAudioBitrate(audioBitrate),
_outputVideoBitrate(videoBitrate)
{
// By default, select the best stream indexes
_inputStreams = _mediaFile.getBestStreams();
}
} // namespace Transcode
+60
View File
@@ -0,0 +1,60 @@
#ifndef TRANSCODE_PARAMETERS_HPP
#define TRANSCODE_PARAMETERS_HPP
#include <string>
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "Format.hpp"
#include "InputMediaFile.hpp"
namespace Transcode
{
class Parameters {
public:
Parameters(const InputMediaFile& InputFile, const Format& outputFormat, std::size_t audioBitrate);
Parameters(const InputMediaFile& InputFile, const Format& outputFormat, std::size_t audioBitrate, std::size_t videoBitrate);
// Modifiers
void setOffset(boost::posix_time::time_duration offset) { _offset = offset; } // Set input offset
void setOutputFormat(const Format& format) { _outputFormat = format; }
// Manually select an input stream to output
// There can be only one stream per each type (video, audio, subtitle)
typedef std::map<Stream::Type, Stream::Id> StreamMap;
void selectInputStream(Stream::Type type, Stream::Id id) { _inputStreams[type] = id; }
const StreamMap& getInputStreams(void) const {return _inputStreams;}
//Accessors
boost::posix_time::time_duration getOffset(void) const {return _offset;}
const Format& getOutputFormat(void) const { return _outputFormat; }
std::size_t getOutputAudioBitrate(void) const { return _outputAudioBitrate; }
std::size_t getOutputVideoBitrate(void) const { return _outputVideoBitrate; }
const InputMediaFile& getInputMediaFile(void) const { return _mediaFile;}
InputMediaFile& getInputMediaFile(void) { return _mediaFile;}
private:
InputMediaFile _mediaFile;
boost::posix_time::time_duration _offset; // start input offset
Format _outputFormat; // OGA, OGV, etc.
std::size_t _outputAudioBitrate; // 192000, 128000, etc.
std::size_t _outputVideoBitrate; // 300000, 700000, etc.
StreamMap _inputStreams;
};
} // namespace Transcode
#endif
+45
View File
@@ -0,0 +1,45 @@
#ifndef TRANSCODE_STREAM_HPP
#define TRANSCODE_STREAM_HPP
#include <string>
namespace Transcode
{
class Stream
{
public:
enum Type
{
Audio,
Video,
Subtitle,
};
typedef std::size_t Id;
Stream(Id id, Type type, const std::string& lang, const std::string& desc)
: _id(id), _type(type), _language(lang), _desc(desc) {}
// Accessors
Id getId() const { return _id;}
Type getType() const { return _type;}
const std::string& getLanguage() const { return _language;}
const std::string& getDesc() const { return _desc;}
private:
Id _id;
Type _type;
std::string _language;
std::string _desc;
};
} // namespace Transcode
#endif