Skip to content

Commit 0f7e5f8

Browse files
committed
Merge pull request #167 from cchampet/dev_newAppAvConcat
New app avconcat
2 parents 92d796a + 3d0aa5e commit 0f7e5f8

File tree

10 files changed

+148
-98
lines changed

10 files changed

+148
-98
lines changed

app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ add_subdirectory(avProcessor)
88
add_subdirectory(pyProcessor)
99
add_subdirectory(pyThumbnail)
1010
add_subdirectory(pyRewrap)
11+
add_subdirectory(pyConcat)

app/pyConcat/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### python/pyConcat
2+
3+
# Install app
4+
install(
5+
FILES "pyconcat.py"
6+
PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE
7+
DESTINATION "bin/python"
8+
)
9+
10+
if(UNIX)
11+
install( CODE "EXECUTE_PROCESS(COMMAND ln -sf python/pyconcat.py ${CMAKE_INSTALL_PREFIX}/bin/pyconcat)" )
12+
endif(UNIX)

app/pyConcat/pyconcat.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python
2+
3+
from pyAvTranscoder import avtranscoder as av
4+
5+
from sets import Set
6+
7+
8+
# Get command line arguments
9+
args = []
10+
try:
11+
# python2.7+
12+
import argparse
13+
14+
# Create command-line interface
15+
parser = argparse.ArgumentParser(
16+
prog='pyconcat',
17+
description='''Concatenate first stream of each given file to create an output file.''',
18+
)
19+
20+
# requirements
21+
parser.add_argument('inputs', nargs='+', action='store', help='list of files to concatenate')
22+
# options
23+
parser.add_argument("-o", "--outputFile", dest="outputFileName", type=str, default="output.mov", help="Set the output filename (output.mov by default).")
24+
# Parse command-line
25+
args = parser.parse_args()
26+
27+
except ImportError:
28+
print("pyconcat currently expects python2.7+")
29+
exit(1)
30+
31+
# setup avtranscoder
32+
logger = av.Logger().setLogLevel(av.AV_LOG_QUIET)
33+
av.preloadCodecsAndFormats()
34+
35+
streamTypeToConcat = Set()
36+
codecToConcat = Set()
37+
# get all input files
38+
inputFiles = []
39+
for input in args.inputs:
40+
inputFile = av.InputFile(input)
41+
streamTypeToConcat.add( inputFile.getStream(0).getProperties().getStreamType() )
42+
codecToConcat.add( inputFile.getStream(0).getProperties().getCodecName() )
43+
inputFiles.append(inputFile)
44+
45+
# Check type of streams to rewrap
46+
if len(streamTypeToConcat) > 1:
47+
raise RuntimeError("Cannot concatenate streams of different type.")
48+
if len(codecToConcat) > 1:
49+
raise RuntimeError("Cannot concatenate streams of different codec: ", [codec for codec in codecToConcat])
50+
51+
# Create the output
52+
outputFile = av.OutputFile( args.outputFileName );
53+
if av.AVMEDIA_TYPE_VIDEO in streamTypeToConcat:
54+
outputFile.addVideoStream( inputFiles[-1].getStream(0).getVideoCodec() )
55+
elif av.AVMEDIA_TYPE_AUDIO in streamTypeToConcat:
56+
outputFile.addVideoStream( inputFiles[-1].getStream(0).getAudioCodec() )
57+
58+
### process
59+
outputFile.beginWrap()
60+
61+
data = av.Frame()
62+
# for each input
63+
for inputFile in inputFiles:
64+
packetRead = True
65+
# read all packets of first stream
66+
while packetRead:
67+
# read
68+
packetRead = inputFile.readNextPacket( data, 0 )
69+
# wrap
70+
outputFile.wrap( data, 0 )
71+
72+
outputFile.endWrap()

app/pyRewrap/pyrewrap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# requirements
1919
parser.add_argument('inputFileName', type=str, help='It could be any video file. Support file without extension.')
2020
# options
21-
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!")
21+
parser.add_argument("-o", "--outputFile", dest="outputFileName", type=str, default="output.mov", help="Set the output filename (output.mov by default).")
2222
parser.add_argument("-c", "--format", dest="format", type=str, default="mov", help="Specify the output format.")
2323
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).")
2424
# Parse command-line
@@ -37,7 +37,7 @@
3737
# requirements
3838
parser.add_option("-i", "--inputFile", dest='inputFileName', type="string", help='It could be any video file. Support file without extension.')
3939
# options
40-
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!")
40+
parser.add_option("-o", "--outputFile", dest="outputFileName", type="string", default="output.mov", help="Set the output filename (output.mov by default).")
4141
parser.add_option("-c", "--format", dest="format", type="string", default="mov", help="Specify the output format.")
4242
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).")
4343
# Parse command-line

src/AvTranscoder/mediaProperty/AudioProperties.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,6 @@ namespace avtranscoder
1616
AudioProperties::AudioProperties( const FormatContext& formatContext, const size_t index )
1717
: StreamProperties( formatContext, index )
1818
{
19-
if( _formatContext )
20-
_codecContext = _formatContext->streams[index]->codec;
21-
22-
if( _formatContext && _codecContext )
23-
_codec = avcodec_find_decoder( _codecContext->codec_id );
24-
}
25-
26-
std::string AudioProperties::getCodecName() const
27-
{
28-
if( ! _codec || ! _codec->name )
29-
throw std::runtime_error( "unknown codec name" );
30-
return std::string( _codec->name );
31-
}
32-
33-
std::string AudioProperties::getCodecLongName() const
34-
{
35-
if( ! _codec || ! _codec->long_name )
36-
throw std::runtime_error( "unknown codec long name" );
37-
return std::string( _codec->long_name );
3819
}
3920

4021
std::string AudioProperties::getSampleFormatName() const
@@ -121,13 +102,6 @@ std::string AudioProperties::getChannelDescription() const
121102
#endif
122103
}
123104

124-
size_t AudioProperties::getCodecId() const
125-
{
126-
if( ! _codecContext )
127-
throw std::runtime_error( "unknown codec context" );
128-
return _codecContext->codec_id;
129-
}
130-
131105
size_t AudioProperties::getSampleRate() const
132106
{
133107
if( ! _codecContext )
@@ -181,9 +155,6 @@ PropertyVector AudioProperties::getPropertiesAsVector() const
181155
PropertyVector basedProperty = StreamProperties::getPropertiesAsVector();
182156
data.insert( data.begin(), basedProperty.begin(), basedProperty.end() );
183157

184-
addProperty( data, "codecId", &AudioProperties::getCodecId );
185-
addProperty( data, "codecName", &AudioProperties::getCodecName );
186-
addProperty( data, "codecLongName", &AudioProperties::getCodecLongName );
187158
addProperty( data, "sampleFormatName", &AudioProperties::getSampleFormatName );
188159
addProperty( data, "sampleFormatLongName", &AudioProperties::getSampleFormatLongName );
189160
addProperty( data, "sampleRate", &AudioProperties::getSampleRate );

src/AvTranscoder/mediaProperty/AudioProperties.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ class AvExport AudioProperties : public StreamProperties
1313
public:
1414
AudioProperties( const FormatContext& formatContext, const size_t index );
1515

16-
std::string getCodecName() const;
17-
std::string getCodecLongName() const;
1816
std::string getSampleFormatName() const;
1917
std::string getSampleFormatLongName() const;
2018
std::string getChannelLayout() const;
2119
std::string getChannelName() const;
2220
std::string getChannelDescription() const;
2321

24-
size_t getCodecId() const;
2522
size_t getSampleRate() const;
2623
size_t getChannels() const;
2724
size_t getBitRate() const; ///< 0 if unknown
@@ -50,10 +47,6 @@ class AvExport AudioProperties : public StreamProperties
5047
}
5148
}
5249
#endif
53-
54-
private:
55-
AVCodecContext* _codecContext; ///< Has link (no ownership)
56-
AVCodec* _codec; ///< Has link (no ownership)
5750
};
5851

5952
}

src/AvTranscoder/mediaProperty/StreamProperties.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@ namespace avtranscoder
77

88
StreamProperties::StreamProperties( const FormatContext& formatContext, const size_t index )
99
: _formatContext( &formatContext.getAVFormatContext() )
10+
, _codecContext( NULL )
11+
, _codec( NULL )
1012
, _streamIndex( index )
1113
{
1214
if( _formatContext )
1315
detail::fillMetadataDictionnary( _formatContext->streams[index]->metadata, _metadatas );
16+
17+
if( _formatContext )
18+
{
19+
if( _streamIndex > _formatContext->nb_streams )
20+
{
21+
std::stringstream ss;
22+
ss << "Stream at index " << _streamIndex << " does not exist.";
23+
throw std::runtime_error( ss.str() );
24+
}
25+
_codecContext = _formatContext->streams[_streamIndex]->codec;
26+
}
27+
28+
if( _formatContext && _codecContext )
29+
_codec = avcodec_find_decoder( _codecContext->codec_id );
1430
}
1531

1632
StreamProperties::~StreamProperties()
@@ -45,6 +61,41 @@ AVMediaType StreamProperties::getStreamType() const
4561
return _formatContext->streams[_streamIndex]->codec->codec_type;
4662
}
4763

64+
size_t StreamProperties::getCodecId() const
65+
{
66+
if( ! _codecContext )
67+
throw std::runtime_error( "unknown codec context" );
68+
return _codecContext->codec_id;
69+
}
70+
71+
std::string StreamProperties::getCodecName() const
72+
{
73+
if( ! _codecContext || ! _codec )
74+
throw std::runtime_error( "unknown codec" );
75+
76+
if( _codec->capabilities & CODEC_CAP_TRUNCATED )
77+
_codecContext->flags |= CODEC_FLAG_TRUNCATED;
78+
79+
if( ! _codec->name )
80+
throw std::runtime_error( "unknown codec name" );
81+
82+
return std::string( _codec->name );
83+
}
84+
85+
std::string StreamProperties::getCodecLongName() const
86+
{
87+
if( ! _codecContext || ! _codec )
88+
throw std::runtime_error( "unknown codec" );
89+
90+
if( _codec->capabilities & CODEC_CAP_TRUNCATED )
91+
_codecContext->flags |= CODEC_FLAG_TRUNCATED;
92+
93+
if( ! _codec->long_name )
94+
throw std::runtime_error( "unknown codec long name" );
95+
96+
return std::string( _codec->long_name );
97+
}
98+
4899
PropertyVector StreamProperties::getPropertiesAsVector() const
49100
{
50101
PropertyVector data;
@@ -53,6 +104,9 @@ PropertyVector StreamProperties::getPropertiesAsVector() const
53104
addProperty( data, "streamIndex", &StreamProperties::getStreamIndex );
54105
addProperty( data, "timeBase", &StreamProperties::getTimeBase );
55106
addProperty( data, "duration", &StreamProperties::getDuration );
107+
addProperty( data, "codecId", &StreamProperties::getCodecId );
108+
addProperty( data, "codecName", &StreamProperties::getCodecName );
109+
addProperty( data, "codecLongName", &StreamProperties::getCodecLongName );
56110

57111
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
58112
{

src/AvTranscoder/mediaProperty/StreamProperties.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class AvExport StreamProperties
2020
Rational getTimeBase() const;
2121
float getDuration() const; ///< in seconds
2222
AVMediaType getStreamType() const;
23+
24+
size_t getCodecId() const;
25+
std::string getCodecName() const;
26+
std::string getCodecLongName() const;
27+
2328
const PropertyVector& getMetadatas() const { return _metadatas; }
2429

2530
#ifndef SWIG
@@ -47,6 +52,8 @@ class AvExport StreamProperties
4752

4853
protected:
4954
const AVFormatContext* _formatContext; ///< Has link (no ownership)
55+
AVCodecContext* _codecContext; ///< Has link (no ownership)
56+
AVCodec* _codec; ///< Has link (no ownership)
5057

5158
size_t _streamIndex;
5259
PropertyVector _metadatas;

src/AvTranscoder/mediaProperty/VideoProperties.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,12 @@ namespace avtranscoder
1717

1818
VideoProperties::VideoProperties( const FormatContext& formatContext, const size_t index, IProgress& progress, const EAnalyseLevel level )
1919
: StreamProperties( formatContext, index )
20-
, _codecContext( NULL )
21-
, _codec( NULL )
2220
, _pixelProperties()
2321
, _isInterlaced( false )
2422
, _isTopFieldFirst( false )
2523
, _gopStructure()
2624
, _firstGopTimeCode( -1 )
2725
{
28-
if( _formatContext )
29-
{
30-
if( _streamIndex > _formatContext->nb_streams )
31-
{
32-
std::stringstream ss;
33-
ss << "video stream at index " << _streamIndex << " does not exist";
34-
throw std::runtime_error( ss.str() );
35-
}
36-
_codecContext = _formatContext->streams[_streamIndex]->codec;
37-
}
38-
39-
if( _formatContext && _codecContext )
40-
_codec = avcodec_find_decoder( _codecContext->codec_id );
41-
4226
if( _codecContext )
4327
{
4428
_pixelProperties = PixelProperties( _codecContext->pix_fmt );
@@ -49,34 +33,6 @@ VideoProperties::VideoProperties( const FormatContext& formatContext, const size
4933
analyseGopStructure( progress );
5034
}
5135

52-
std::string VideoProperties::getCodecName() const
53-
{
54-
if( ! _codecContext || ! _codec )
55-
throw std::runtime_error( "unknown codec" );
56-
57-
if( _codec->capabilities & CODEC_CAP_TRUNCATED )
58-
_codecContext->flags|= CODEC_FLAG_TRUNCATED;
59-
60-
if( ! _codec->name )
61-
throw std::runtime_error( "unknown codec name" );
62-
63-
return std::string( _codec->name );
64-
}
65-
66-
std::string VideoProperties::getCodecLongName() const
67-
{
68-
if( ! _codecContext || ! _codec )
69-
throw std::runtime_error( "unknown codec" );
70-
71-
if( _codec->capabilities & CODEC_CAP_TRUNCATED )
72-
_codecContext->flags|= CODEC_FLAG_TRUNCATED;
73-
74-
if( ! _codec->long_name )
75-
throw std::runtime_error( "unknown codec long name" );
76-
77-
return std::string( _codec->long_name );
78-
}
79-
8036
std::string VideoProperties::getProfileName() const
8137
{
8238
if( ! _codecContext || ! _codec )
@@ -365,13 +321,6 @@ Rational VideoProperties::getDar() const
365321
return dar;
366322
}
367323

368-
size_t VideoProperties::getCodecId() const
369-
{
370-
if( ! _codecContext )
371-
throw std::runtime_error( "unknown codec context" );
372-
return _codecContext->codec_id;
373-
}
374-
375324
size_t VideoProperties::getBitRate() const
376325
{
377326
if( ! _codecContext )
@@ -606,9 +555,6 @@ PropertyVector VideoProperties::getPropertiesAsVector() const
606555
PropertyVector basedProperty = StreamProperties::getPropertiesAsVector();
607556
data.insert( data.begin(), basedProperty.begin(), basedProperty.end() );
608557

609-
addProperty( data, "codecId", &VideoProperties::getCodecId );
610-
addProperty( data, "codecName", &VideoProperties::getCodecName );
611-
addProperty( data, "codecLongName", &VideoProperties::getCodecLongName );
612558
addProperty( data, "profile", &VideoProperties::getProfile );
613559
addProperty( data, "profileName", &VideoProperties::getProfileName );
614560
addProperty( data, "level", &VideoProperties::getLevel );

0 commit comments

Comments
 (0)