Skip to content

Commit 189a1bc

Browse files
committed
Merge pull request #232 from cchampet/fix_thumbnailOfImage
Fix generation of thumbnail from images
2 parents 9bf2fb9 + 38b4f4e commit 189a1bc

File tree

12 files changed

+131
-16
lines changed

12 files changed

+131
-16
lines changed

app/pyRewrap/pyrewrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
exit(1)
4949

5050
# setup avtranscoder
51-
logger = av.Logger().setLogLevel(av.AV_LOG_QUIET)
51+
av.Logger().setLogLevel(av.AV_LOG_QUIET)
5252
av.preloadCodecsAndFormats()
5353

5454
# create input file

app/pyThumbnail/pythumbnail.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454

5555
# setup avtranscoder
56-
logger = av.Logger().setLogLevel(av.AV_LOG_QUIET)
56+
av.Logger().setLogLevel(av.AV_LOG_QUIET)
5757
av.preloadCodecsAndFormats()
5858

5959
# create input file
@@ -97,10 +97,10 @@
9797

9898
# create transcoder
9999
transcoder = av.Transcoder( outputFile )
100-
transcoder.add( outputStream );
100+
transcoder.add( outputStream )
101101

102102
# launch process
103-
outputFile.beginWrap();
104-
transcoder.preProcessCodecLatency();
105-
transcoder.processFrame();
106-
outputFile.endWrap();
103+
outputFile.beginWrap()
104+
transcoder.preProcessCodecLatency()
105+
transcoder.processFrame()
106+
outputFile.endWrap()

src/AvTranscoder/decoder/AudioDecoder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ bool AudioDecoder::decodeNextFrame(Frame& frameBuffer)
8989

9090
const bool nextPacketRead = _inputStream->readNextPacket(data);
9191
// if error or end of file
92-
if(!nextPacketRead)
92+
if(!nextPacketRead && !decodeNextFrame)
9393
{
9494
data.clear();
9595
return false;
9696
}
9797

9898
// decoding
99+
// @note could be called several times to return the remaining frames (last call with an empty packet)
100+
// @see CODEC_CAP_DELAY
99101
int ret = avcodec_decode_audio4(&_inputStream->getAudioCodec().getAVCodecContext(), &frameBuffer.getAVFrame(),
100102
&got_frame, &data.getAVPacket());
101103
if(ret < 0)

src/AvTranscoder/decoder/VideoDecoder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ bool VideoDecoder::decodeNextFrame(Frame& frameBuffer)
8787

8888
const bool nextPacketRead = _inputStream->readNextPacket(data);
8989
// if error or end of file
90-
if(!nextPacketRead)
90+
if(!nextPacketRead && !decodeNextFrame)
9191
{
9292
data.clear();
9393
return false;
9494
}
9595

9696
// decoding
97+
// @note could be called several times to return the remaining frames (last call with an empty packet)
98+
// @see CODEC_CAP_DELAY
9799
const int ret = avcodec_decode_video2(&_inputStream->getVideoCodec().getAVCodecContext(), &frameBuffer.getAVFrame(),
98100
&got_frame, &data.getAVPacket());
99101
if(ret < 0)

src/AvTranscoder/file/InputFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ bool InputFile::readNextPacket(CodedData& data, const size_t streamIndex)
8080
const int packetStreamIndex = data.getAVPacket().stream_index;
8181
if(packetStreamIndex == (int)streamIndex)
8282
{
83-
LOG_DEBUG("Get a packet data of the stream " << streamIndex)
83+
LOG_DEBUG("Get a packet from stream " << streamIndex)
8484
nextPacketFound = true;
8585
}
8686
// else add the packet data to the stream cache

src/AvTranscoder/properties/util.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "util.hpp"
22

3+
extern "C" {
4+
#include <libavutil/avutil.h>
5+
}
6+
37
#include <sstream>
48

59
namespace avtranscoder
@@ -8,6 +12,28 @@ namespace avtranscoder
812
namespace detail
913
{
1014

15+
template <>
16+
void add(PropertyVector& propertyVector, const std::string& key, const size_t& value)
17+
{
18+
std::stringstream ss;
19+
if(value == (size_t)AV_NOPTS_VALUE)
20+
ss << "N/A";
21+
else
22+
ss << value;
23+
add(propertyVector, key, ss.str());
24+
}
25+
26+
template <>
27+
void add(PropertyVector& propertyVector, const std::string& key, const float& value)
28+
{
29+
std::stringstream ss;
30+
if(value <= AV_NOPTS_VALUE || value >= AV_NOPTS_VALUE)
31+
ss << "N/A";
32+
else
33+
ss << value;
34+
add(propertyVector, key, ss.str());
35+
}
36+
1137
template <>
1238
void add(PropertyVector& propertyVector, const std::string& key, const std::string& value)
1339
{

src/AvTranscoder/properties/util.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ void add(PropertyVector& propertyVector, const std::string& key, const T& value)
4545
add(propertyVector, key, ss.str());
4646
}
4747

48+
template <>
49+
void add(PropertyVector& propertyVector, const std::string& key, const size_t& value);
50+
51+
template <>
52+
void add(PropertyVector& propertyVector, const std::string& key, const float& value);
53+
4854
template <>
4955
void add(PropertyVector& propertyVector, const std::string& key, const std::string& value);
5056

src/AvTranscoder/stream/InputStream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ bool InputStream::readNextPacket(CodedData& data)
5858
// if packet is already cached
5959
if(!_streamCache.empty())
6060
{
61-
LOG_DEBUG("Get packet data of stream " << _streamIndex << " from the cache")
61+
LOG_DEBUG("Get packet of '" << _inputFile->getFilename() << "' (stream " << _streamIndex << ") from the cache")
6262
data.copyData(_streamCache.front().getData(), _streamCache.front().getSize());
6363
_streamCache.pop();
6464
}
6565
// else read next packet
6666
else
6767
{
68-
LOG_DEBUG("Read next packet")
68+
LOG_DEBUG("Read next packet of '" << _inputFile->getFilename() << "' (stream " << _streamIndex << ")")
6969
return _inputFile->readNextPacket(data, _streamIndex) && _streamCache.empty();
7070
}
7171

src/AvTranscoder/transcoder/Transcoder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ ProcessStat Transcoder::process(IProgress& progress)
289289
bool frameProcessed = true;
290290
while(frameProcessed)
291291
{
292+
LOG_DEBUG("Process frame " << frame)
293+
frameProcessed = processFrame();
294+
++frame;
295+
292296
const float progressDuration = getCurrentOutputDuration();
293297

294298
// check if JobStatusCancel
@@ -306,10 +310,6 @@ ProcessStat Transcoder::process(IProgress& progress)
306310
<< progressDuration << "s) is equal or upper than " << expectedOutputDuration << "s.")
307311
break;
308312
}
309-
310-
LOG_DEBUG("Process frame " << frame)
311-
frameProcessed = processFrame();
312-
++frame;
313313
}
314314

315315
_outputFile.endWrap();
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
3+
# Check if environment is setup to run the tests
4+
if os.environ.get('AVTRANSCODER_TEST_IMAGE_PNG_FILE') is None or \
5+
os.environ.get('AVTRANSCODER_TEST_IMAGE_JPG_FILE') is None:
6+
from nose.plugins.skip import SkipTest
7+
raise SkipTest("Need to define environment variables "
8+
"AVTRANSCODER_TEST_IMAGE_PNG_FILE and "
9+
"AVTRANSCODER_TEST_IMAGE_JPG_FILE")
10+
11+
from nose.tools import *
12+
13+
from pyAvTranscoder import avtranscoder as av
14+
15+
16+
def testTranscodePngToMjpeg():
17+
"""
18+
Transcode one image (to mjpeg).
19+
"""
20+
inputFileName = os.environ['AVTRANSCODER_TEST_IMAGE_PNG_FILE']
21+
outputFileName = "testTranscodePngToMjpeg.jpg"
22+
23+
ouputFile = av.OutputFile( outputFileName )
24+
transcoder = av.Transcoder( ouputFile )
25+
26+
inputFile = av.InputFile( inputFileName )
27+
src_videoStream = inputFile.getProperties().getVideoProperties()[0]
28+
videoStreamIndex = src_videoStream.getStreamIndex()
29+
transcoder.add( inputFileName, videoStreamIndex, "mjpeg" )
30+
31+
progress = av.ConsoleProgress()
32+
processStat = transcoder.process( progress )
33+
34+
# check process stat returned
35+
videoStat = processStat.getVideoStat(0)
36+
assert_equals(1, videoStat.getNbFrames())
37+
38+
# get dst file of transcode
39+
dst_inputFile = av.InputFile( outputFileName )
40+
dst_properties = dst_inputFile.getProperties()
41+
dst_videoStream = dst_properties.getVideoProperties()[0]
42+
43+
assert_equals( "mjpeg", dst_videoStream.getCodecName() )
44+
assert_equals( "yuvj420p", dst_videoStream.getPixelProperties().getPixelName() )
45+
46+
47+
def testTranscodeJpgToMjpeg():
48+
"""
49+
Transcode one image (to mjpeg).
50+
"""
51+
inputFileName = os.environ['AVTRANSCODER_TEST_IMAGE_JPG_FILE']
52+
outputFileName = "testTranscodeJpgToMjpeg.jpg"
53+
54+
ouputFile = av.OutputFile( outputFileName )
55+
transcoder = av.Transcoder( ouputFile )
56+
57+
inputFile = av.InputFile( inputFileName )
58+
src_videoStream = inputFile.getProperties().getVideoProperties()[0]
59+
videoStreamIndex = src_videoStream.getStreamIndex()
60+
transcoder.add( inputFileName, videoStreamIndex, "mjpeg" )
61+
62+
progress = av.ConsoleProgress()
63+
processStat = transcoder.process( progress )
64+
65+
# check process stat returned
66+
videoStat = processStat.getVideoStat(0)
67+
assert_equals(1, videoStat.getNbFrames())
68+
69+
# get dst file of transcode
70+
dst_inputFile = av.InputFile( outputFileName )
71+
dst_properties = dst_inputFile.getProperties()
72+
dst_videoStream = dst_properties.getVideoProperties()[0]
73+
74+
assert_equals( "mjpeg", dst_videoStream.getCodecName() )
75+
assert_equals( "yuvj420p", dst_videoStream.getPixelProperties().getPixelName() )

tools/appveyor/python.nosetests.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ set AVTRANSCODER_TEST_VIDEO_MP4_FILE=%PWD%\avTranscoder-data\video\BigBuckBunny\
1616
set AVTRANSCODER_TEST_VIDEO_MOV_FILE=%PWD%\avTranscoder-data\video\BigBuckBunny\BigBuckBunny_640p_h264.mov
1717
set AVTRANSCODER_TEST_AUDIO_WAVE_FILE=%PWD%\avTranscoder-data\audio\frequenciesPerChannel.wav
1818
set AVTRANSCODER_TEST_AUDIO_MOV_FILE=%PWD%\avTranscoder-data\video\BigBuckBunny\BigBuckBunny_1080p_5_1.mov
19+
set AVTRANSCODER_TEST_IMAGE_PNG_FILE=%PWD%\avTranscoder-data\image\BigBuckBunny\bbb-splash.thumbnail.png
20+
set AVTRANSCODER_TEST_IMAGE_JPG_FILE=%PWD%\avTranscoder-data\image\BigBuckBunny\title_anouncement.thumbnail.jpg
1921

2022
:: Launch tests
2123
cd test\pyTest

tools/travis/python.nosetests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export AVTRANSCODER_TEST_VIDEO_MP4_FILE=`pwd`/avTranscoder-data/video/BigBuckBun
1414
export AVTRANSCODER_TEST_VIDEO_MOV_FILE=`pwd`/avTranscoder-data/video/BigBuckBunny/BigBuckBunny_640p_h264.mov
1515
export AVTRANSCODER_TEST_AUDIO_WAVE_FILE=`pwd`/avTranscoder-data/audio/frequenciesPerChannel.wav
1616
export AVTRANSCODER_TEST_AUDIO_MOV_FILE=`pwd`/avTranscoder-data/video/BigBuckBunny/BigBuckBunny_1080p_5_1.mov
17+
export AVTRANSCODER_TEST_IMAGE_PNG_FILE=`pwd`/avTranscoder-data/image/BigBuckBunny/bbb-splash.thumbnail.png
18+
export AVTRANSCODER_TEST_IMAGE_JPG_FILE=`pwd`/avTranscoder-data/image/BigBuckBunny/title_anouncement.thumbnail.jpg
1719

1820
# Launch tests
1921
nosetests ${TRAVIS_BUILD_DIR}/test/pyTest --with-coverage

0 commit comments

Comments
 (0)