From 9cf1dbd54509e7b1f1198845b3e636d9be489012 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Wed, 9 Nov 2016 23:43:00 +0300 Subject: [PATCH] added example of saving preview of video file in png picture --- examples/CMakeLists.txt | 8 +++++-- examples/ffPreview.cpp | 48 +++++++++++++++++++++++++++++++++++++++++ include/ffcpp/Frame.h | 2 ++ src/Frame.cpp | 8 +++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 examples/ffPreview.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index cf7db79..8c9d2ff 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,9 @@ project(ffConv) - add_executable(ffConv ffConv.cpp) add_dependencies(ffConv ffcpp) -target_link_libraries(ffConv ffcpp) \ No newline at end of file +target_link_libraries(ffConv ffcpp) + +project(ffPreview) +add_executable(ffPreview ffPreview.cpp) +add_dependencies(ffPreview ffcpp) +target_link_libraries(ffPreview ffcpp) \ No newline at end of file diff --git a/examples/ffPreview.cpp b/examples/ffPreview.cpp new file mode 100644 index 0000000..6438481 --- /dev/null +++ b/examples/ffPreview.cpp @@ -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; +} \ No newline at end of file diff --git a/include/ffcpp/Frame.h b/include/ffcpp/Frame.h index 354cc51..1de8b31 100644 --- a/include/ffcpp/Frame.h +++ b/include/ffcpp/Frame.h @@ -28,6 +28,8 @@ namespace ffcpp { void setPictureType(AVPictureType type); int samplesCount() const; void setPts(int pts); + bool isKeyFrame() const; + int pts() const; }; } diff --git a/src/Frame.cpp b/src/Frame.cpp index 6035ba6..c243d2f 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -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; + } + }