added example of saving preview of video file in png picture
This commit is contained in:
parent
18123eacb5
commit
9cf1dbd545
@ -1,5 +1,9 @@
|
||||
project(ffConv)
|
||||
|
||||
add_executable(ffConv ffConv.cpp)
|
||||
add_dependencies(ffConv ffcpp)
|
||||
target_link_libraries(ffConv ffcpp)
|
||||
|
||||
project(ffPreview)
|
||||
add_executable(ffPreview ffPreview.cpp)
|
||||
add_dependencies(ffPreview ffcpp)
|
||||
target_link_libraries(ffPreview ffcpp)
|
||||
48
examples/ffPreview.cpp
Normal file
48
examples/ffPreview.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include "ffcpp/ffcpp.h"
|
||||
#include "ffcpp/MediaFile.h"
|
||||
#include "ffcpp/Scaler.h"
|
||||
|
||||
namespace ff = ffcpp;
|
||||
|
||||
size_t KEY_FRAME_TO_SAVE = 0;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
ff::init();
|
||||
ff::MediaFile input(argv[1], ff::Mode::Read);
|
||||
ff::MediaFile output(argv[2], ff::Mode::Write);
|
||||
|
||||
ff::Stream& vStream = input.videoStream();
|
||||
ff::Codec& vDecoder = vStream.codec();
|
||||
|
||||
ff::Stream& outVStream = output.addVideoStream(AV_CODEC_ID_PNG, vDecoder.width(), vDecoder.height(), vStream.timeBase(), AV_PIX_FMT_RGB24);
|
||||
ff::Codec& vEncoder = outVStream.codec();
|
||||
|
||||
output.writeHeader();
|
||||
|
||||
size_t curKeyFrame = 0;
|
||||
ff::Scaler scaler(vDecoder, vEncoder);
|
||||
while(auto packet = input.readPacket()) {
|
||||
AVMediaType packetType = input.packetType(packet);
|
||||
if(packetType == AVMEDIA_TYPE_VIDEO) {
|
||||
auto frame = vDecoder.decode(packet);
|
||||
if(frame.isKeyFrame() && (frame.pts() > 0 || KEY_FRAME_TO_SAVE == 0)) {
|
||||
if(curKeyFrame == KEY_FRAME_TO_SAVE) {
|
||||
frame = scaler.scale(frame);
|
||||
frame.setPictureType(AV_PICTURE_TYPE_NONE);
|
||||
auto encPacket = vEncoder.encode(frame);
|
||||
if(!encPacket) continue;
|
||||
encPacket.setStreamIndex(0);
|
||||
encPacket.rescaleTimestamps(vStream.timeBase(), outVStream.timeBase());
|
||||
output.writePacket(encPacket);
|
||||
break;
|
||||
} else {
|
||||
++curKeyFrame;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output.writeTrailer();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -28,6 +28,8 @@ namespace ffcpp {
|
||||
void setPictureType(AVPictureType type);
|
||||
int samplesCount() const;
|
||||
void setPts(int pts);
|
||||
bool isKeyFrame() const;
|
||||
int pts() const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -74,4 +74,12 @@ namespace ffcpp {
|
||||
_frame->pts = pts;
|
||||
}
|
||||
|
||||
bool Frame::isKeyFrame() const {
|
||||
return (_frame->key_frame == 1);
|
||||
}
|
||||
|
||||
int Frame::pts() const {
|
||||
return _frame->pts;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user