Skip to content

New app avconcat #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ add_subdirectory(avProcessor)
add_subdirectory(pyProcessor)
add_subdirectory(pyThumbnail)
add_subdirectory(pyRewrap)
add_subdirectory(pyConcat)
12 changes: 12 additions & 0 deletions app/pyConcat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### python/pyConcat

# Install app
install(
FILES "pyconcat.py"
PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE
DESTINATION "bin/python"
)

if(UNIX)
install( CODE "EXECUTE_PROCESS(COMMAND ln -sf python/pyconcat.py ${CMAKE_INSTALL_PREFIX}/bin/pyconcat)" )
endif(UNIX)
72 changes: 72 additions & 0 deletions app/pyConcat/pyconcat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python

from pyAvTranscoder import avtranscoder as av

from sets import Set


# Get command line arguments
args = []
try:
# python2.7+
import argparse

# Create command-line interface
parser = argparse.ArgumentParser(
prog='pyconcat',
description='''Concatenate first stream of each given file to create an output file.''',
)

# requirements
parser.add_argument('inputs', nargs='+', action='store', help='list of files to concatenate')
# options
parser.add_argument("-o", "--outputFile", dest="outputFileName", type=str, default="output.mov", help="Set the output filename (output.mov by default).")
# Parse command-line
args = parser.parse_args()

except ImportError:
print("pyconcat currently expects python2.7+")
exit(1)

# setup avtranscoder
logger = av.Logger().setLogLevel(av.AV_LOG_QUIET)
av.preloadCodecsAndFormats()

streamTypeToConcat = Set()
codecToConcat = Set()
# get all input files
inputFiles = []
for input in args.inputs:
inputFile = av.InputFile(input)
streamTypeToConcat.add( inputFile.getStream(0).getProperties().getStreamType() )
codecToConcat.add( inputFile.getStream(0).getProperties().getCodecName() )
inputFiles.append(inputFile)

# Check type of streams to rewrap
if len(streamTypeToConcat) > 1:
raise RuntimeError("Cannot concatenate streams of different type.")
if len(codecToConcat) > 1:
raise RuntimeError("Cannot concatenate streams of different codec: ", [codec for codec in codecToConcat])

# Create the output
outputFile = av.OutputFile( args.outputFileName );
if av.AVMEDIA_TYPE_VIDEO in streamTypeToConcat:
outputFile.addVideoStream( inputFiles[-1].getStream(0).getVideoCodec() )
elif av.AVMEDIA_TYPE_AUDIO in streamTypeToConcat:
outputFile.addVideoStream( inputFiles[-1].getStream(0).getAudioCodec() )

### process
outputFile.beginWrap()

data = av.Frame()
# for each input
for inputFile in inputFiles:
packetRead = True
# read all packets of first stream
while packetRead:
# read
packetRead = inputFile.readNextPacket( data, 0 )
# wrap
outputFile.wrap( data, 0 )

outputFile.endWrap()
4 changes: 2 additions & 2 deletions app/pyRewrap/pyrewrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# requirements
parser.add_argument('inputFileName', type=str, help='It could be any video file. Support file without extension.')
# options
parser.add_argument("-o", "--outputFile", dest="outputFileName", type=str, default="output.mov", help="Set the output filename (thumbnail.jpg by default). Must be with jpg extension!")
parser.add_argument("-o", "--outputFile", dest="outputFileName", type=str, default="output.mov", help="Set the output filename (output.mov by default).")
parser.add_argument("-c", "--format", dest="format", type=str, default="mov", help="Specify the output format.")
parser.add_argument("-f", "--faststart", dest="faststart", action="store_true", default=False, help="Specify if the faststart option must be apply during rewrapping process (warning: 'mov' specific option).")
# Parse command-line
Expand All @@ -37,7 +37,7 @@
# requirements
parser.add_option("-i", "--inputFile", dest='inputFileName', type="string", help='It could be any video file. Support file without extension.')
# options
parser.add_option("-o", "--outputFile", dest="outputFileName", type="string", default="output.mov", help="Set the output filename (thumbnail.jpg by default). Must be with jpg extension!")
parser.add_option("-o", "--outputFile", dest="outputFileName", type="string", default="output.mov", help="Set the output filename (output.mov by default).")
parser.add_option("-c", "--format", dest="format", type="string", default="mov", help="Specify the output format.")
parser.add_option("-f", "--faststart", dest="faststart", action="store_true", default=False, help="Specify if the faststart option must be apply during rewrapping process (warning: 'mov' specific option).")
# Parse command-line
Expand Down
29 changes: 0 additions & 29 deletions src/AvTranscoder/mediaProperty/AudioProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,6 @@ namespace avtranscoder
AudioProperties::AudioProperties( const FormatContext& formatContext, const size_t index )
: StreamProperties( formatContext, index )
{
if( _formatContext )
_codecContext = _formatContext->streams[index]->codec;

if( _formatContext && _codecContext )
_codec = avcodec_find_decoder( _codecContext->codec_id );
}

std::string AudioProperties::getCodecName() const
{
if( ! _codec || ! _codec->name )
throw std::runtime_error( "unknown codec name" );
return std::string( _codec->name );
}

std::string AudioProperties::getCodecLongName() const
{
if( ! _codec || ! _codec->long_name )
throw std::runtime_error( "unknown codec long name" );
return std::string( _codec->long_name );
}

std::string AudioProperties::getSampleFormatName() const
Expand Down Expand Up @@ -121,13 +102,6 @@ std::string AudioProperties::getChannelDescription() const
#endif
}

size_t AudioProperties::getCodecId() const
{
if( ! _codecContext )
throw std::runtime_error( "unknown codec context" );
return _codecContext->codec_id;
}

size_t AudioProperties::getSampleRate() const
{
if( ! _codecContext )
Expand Down Expand Up @@ -181,9 +155,6 @@ PropertyVector AudioProperties::getPropertiesAsVector() const
PropertyVector basedProperty = StreamProperties::getPropertiesAsVector();
data.insert( data.begin(), basedProperty.begin(), basedProperty.end() );

addProperty( data, "codecId", &AudioProperties::getCodecId );
addProperty( data, "codecName", &AudioProperties::getCodecName );
addProperty( data, "codecLongName", &AudioProperties::getCodecLongName );
addProperty( data, "sampleFormatName", &AudioProperties::getSampleFormatName );
addProperty( data, "sampleFormatLongName", &AudioProperties::getSampleFormatLongName );
addProperty( data, "sampleRate", &AudioProperties::getSampleRate );
Expand Down
7 changes: 0 additions & 7 deletions src/AvTranscoder/mediaProperty/AudioProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ class AvExport AudioProperties : public StreamProperties
public:
AudioProperties( const FormatContext& formatContext, const size_t index );

std::string getCodecName() const;
std::string getCodecLongName() const;
std::string getSampleFormatName() const;
std::string getSampleFormatLongName() const;
std::string getChannelLayout() const;
std::string getChannelName() const;
std::string getChannelDescription() const;

size_t getCodecId() const;
size_t getSampleRate() const;
size_t getChannels() const;
size_t getBitRate() const; ///< 0 if unknown
Expand Down Expand Up @@ -50,10 +47,6 @@ class AvExport AudioProperties : public StreamProperties
}
}
#endif

private:
AVCodecContext* _codecContext; ///< Has link (no ownership)
AVCodec* _codec; ///< Has link (no ownership)
};

}
Expand Down
54 changes: 54 additions & 0 deletions src/AvTranscoder/mediaProperty/StreamProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ namespace avtranscoder

StreamProperties::StreamProperties( const FormatContext& formatContext, const size_t index )
: _formatContext( &formatContext.getAVFormatContext() )
, _codecContext( NULL )
, _codec( NULL )
, _streamIndex( index )
{
if( _formatContext )
detail::fillMetadataDictionnary( _formatContext->streams[index]->metadata, _metadatas );

if( _formatContext )
{
if( _streamIndex > _formatContext->nb_streams )
{
std::stringstream ss;
ss << "Stream at index " << _streamIndex << " does not exist.";
throw std::runtime_error( ss.str() );
}
_codecContext = _formatContext->streams[_streamIndex]->codec;
}

if( _formatContext && _codecContext )
_codec = avcodec_find_decoder( _codecContext->codec_id );
}

StreamProperties::~StreamProperties()
Expand Down Expand Up @@ -50,6 +66,41 @@ AVMediaType StreamProperties::getStreamType() const
return _formatContext->streams[_streamIndex]->codec->codec_type;
}

size_t StreamProperties::getCodecId() const
{
if( ! _codecContext )
throw std::runtime_error( "unknown codec context" );
return _codecContext->codec_id;
}

std::string StreamProperties::getCodecName() const
{
if( ! _codecContext || ! _codec )
throw std::runtime_error( "unknown codec" );

if( _codec->capabilities & CODEC_CAP_TRUNCATED )
_codecContext->flags |= CODEC_FLAG_TRUNCATED;

if( ! _codec->name )
throw std::runtime_error( "unknown codec name" );

return std::string( _codec->name );
}

std::string StreamProperties::getCodecLongName() const
{
if( ! _codecContext || ! _codec )
throw std::runtime_error( "unknown codec" );

if( _codec->capabilities & CODEC_CAP_TRUNCATED )
_codecContext->flags |= CODEC_FLAG_TRUNCATED;

if( ! _codec->long_name )
throw std::runtime_error( "unknown codec long name" );

return std::string( _codec->long_name );
}

PropertyVector StreamProperties::getPropertiesAsVector() const
{
PropertyVector data;
Expand All @@ -58,6 +109,9 @@ PropertyVector StreamProperties::getPropertiesAsVector() const
addProperty( data, "streamIndex", &StreamProperties::getStreamIndex );
addProperty( data, "timeBase", &StreamProperties::getTimeBase );
addProperty( data, "duration", &StreamProperties::getDuration );
addProperty( data, "codecId", &StreamProperties::getCodecId );
addProperty( data, "codecName", &StreamProperties::getCodecName );
addProperty( data, "codecLongName", &StreamProperties::getCodecLongName );

for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
{
Expand Down
7 changes: 7 additions & 0 deletions src/AvTranscoder/mediaProperty/StreamProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class AvExport StreamProperties
Rational getTimeBase() const;
float getDuration() const; ///< in seconds
AVMediaType getStreamType() const;

size_t getCodecId() const;
std::string getCodecName() const;
std::string getCodecLongName() const;

const PropertyVector& getMetadatas() const { return _metadatas; }

#ifndef SWIG
Expand Down Expand Up @@ -47,6 +52,8 @@ class AvExport StreamProperties

protected:
const AVFormatContext* _formatContext; ///< Has link (no ownership)
AVCodecContext* _codecContext; ///< Has link (no ownership)
AVCodec* _codec; ///< Has link (no ownership)

size_t _streamIndex;
PropertyVector _metadatas;
Expand Down
54 changes: 0 additions & 54 deletions src/AvTranscoder/mediaProperty/VideoProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,12 @@ namespace avtranscoder

VideoProperties::VideoProperties( const FormatContext& formatContext, const size_t index, IProgress& progress, const EAnalyseLevel level )
: StreamProperties( formatContext, index )
, _codecContext( NULL )
, _codec( NULL )
, _pixelProperties()
, _isInterlaced( false )
, _isTopFieldFirst( false )
, _gopStructure()
, _firstGopTimeCode( -1 )
{
if( _formatContext )
{
if( _streamIndex > _formatContext->nb_streams )
{
std::stringstream ss;
ss << "video stream at index " << _streamIndex << " does not exist";
throw std::runtime_error( ss.str() );
}
_codecContext = _formatContext->streams[_streamIndex]->codec;
}

if( _formatContext && _codecContext )
_codec = avcodec_find_decoder( _codecContext->codec_id );

if( _codecContext )
{
_pixelProperties = PixelProperties( _codecContext->pix_fmt );
Expand All @@ -49,34 +33,6 @@ VideoProperties::VideoProperties( const FormatContext& formatContext, const size
analyseGopStructure( progress );
}

std::string VideoProperties::getCodecName() const
{
if( ! _codecContext || ! _codec )
throw std::runtime_error( "unknown codec" );

if( _codec->capabilities & CODEC_CAP_TRUNCATED )
_codecContext->flags|= CODEC_FLAG_TRUNCATED;

if( ! _codec->name )
throw std::runtime_error( "unknown codec name" );

return std::string( _codec->name );
}

std::string VideoProperties::getCodecLongName() const
{
if( ! _codecContext || ! _codec )
throw std::runtime_error( "unknown codec" );

if( _codec->capabilities & CODEC_CAP_TRUNCATED )
_codecContext->flags|= CODEC_FLAG_TRUNCATED;

if( ! _codec->long_name )
throw std::runtime_error( "unknown codec long name" );

return std::string( _codec->long_name );
}

std::string VideoProperties::getProfileName() const
{
if( ! _codecContext || ! _codec )
Expand Down Expand Up @@ -365,13 +321,6 @@ Rational VideoProperties::getDar() const
return dar;
}

size_t VideoProperties::getCodecId() const
{
if( ! _codecContext )
throw std::runtime_error( "unknown codec context" );
return _codecContext->codec_id;
}

size_t VideoProperties::getBitRate() const
{
if( ! _codecContext )
Expand Down Expand Up @@ -606,9 +555,6 @@ PropertyVector VideoProperties::getPropertiesAsVector() const
PropertyVector basedProperty = StreamProperties::getPropertiesAsVector();
data.insert( data.begin(), basedProperty.begin(), basedProperty.end() );

addProperty( data, "codecId", &VideoProperties::getCodecId );
addProperty( data, "codecName", &VideoProperties::getCodecName );
addProperty( data, "codecLongName", &VideoProperties::getCodecLongName );
addProperty( data, "profile", &VideoProperties::getProfile );
addProperty( data, "profileName", &VideoProperties::getProfileName );
addProperty( data, "level", &VideoProperties::getLevel );
Expand Down
Loading