| 1 |
//Inspired from QTFFmpegWrapper (Daniel Roggen) and the 'muxing.c' example in FFmpeg doc/examples |
| 2 |
#ifndef Q_VIDEO_ENCODE_HEADER |
| 3 |
#define Q_VIDEO_ENCODE_HEADER |
| 4 |
|
| 5 |
//Qt |
| 6 |
#include <QImage> |
| 7 |
|
| 8 |
//FFmpeg variables (we don't want to pollute the client project with FFmpeg includes!) |
| 9 |
struct FFmpegStuffEnc; |
| 10 |
|
| 11 |
//! Video encoder based on FFmpeg |
| 12 |
class QVideoEncoder |
| 13 |
{ |
| 14 |
public: |
| 15 |
|
| 16 |
//! Default constructor |
| 17 |
/** \param filename video filename |
| 18 |
\param width video width (must be a multiple of 8) |
| 19 |
\param height video height (must be a multiple of 8) |
| 20 |
\param bitrate bit rate (e.g. 400 000) |
| 21 |
\param gop keyframe interval |
| 22 |
\param fps frame rate |
| 23 |
**/ |
| 24 |
QVideoEncoder( QString filename, |
| 25 |
int width, |
| 26 |
int height, |
| 27 |
unsigned bitrate, |
| 28 |
int gop = 12, |
| 29 |
int fps = 25); |
| 30 |
|
| 31 |
virtual ~QVideoEncoder(); |
| 32 |
|
| 33 |
//! Creates an (empty) video/anmiation file |
| 34 |
/** \param formatShortName output format (short name) |
| 35 |
- If empty, the format will be automatically guessed from the filename. |
| 36 |
- If it can't be guessed this way then MPEG is used by default. |
| 37 |
\param errors (if any) |
| 38 |
\return success |
| 39 |
**/ |
| 40 |
bool open(QString formatShortName, QStringList& errors); |
| 41 |
|
| 42 |
//! Returns whether the file is opened or not |
| 43 |
inline bool isOpen() const { return m_isOpen; } |
| 44 |
|
| 45 |
//! Adds an image to the stream |
| 46 |
virtual bool encodeImage(const QImage& image, int frameIndex, QString* errorString = nullptr); |
| 47 |
|
| 48 |
//! Closes the file |
| 49 |
virtual bool close(); |
| 50 |
|
| 51 |
//! Output format |
| 52 |
struct OutputFormat |
| 53 |
{ |
| 54 |
QString shortName; |
| 55 |
QString longName; |
| 56 |
QString extensions; |
| 57 |
}; |
| 58 |
|
| 59 |
//! Returns the list of supported output formats |
| 60 |
static bool GetSupportedOutputFormats(std::vector<OutputFormat>& formats, bool ignoreIfNoFileExtension = true); |
| 61 |
|
| 62 |
protected: |
| 63 |
|
| 64 |
//stream descriptor |
| 65 |
QString m_filename; |
| 66 |
int m_width; |
| 67 |
int m_height; |
| 68 |
unsigned m_bitrate; |
| 69 |
int m_gop; |
| 70 |
int m_fps; |
| 71 |
bool m_isOpen; |
| 72 |
|
| 73 |
//! FFmpeg variables |
| 74 |
FFmpegStuffEnc* m_ff; |
| 75 |
|
| 76 |
//! Returns whether the image size is valid |
| 77 |
bool isSizeValid(); |
| 78 |
|
| 79 |
// Alloc/free a frame |
| 80 |
bool initFrame(); |
| 81 |
void freeFrame(); |
| 82 |
|
| 83 |
//! Convert the QImage to the internal YUV format |
| 84 |
/** \warning The QImage is allocated by QT without guarantee about the alignment and bytes per lines. |
| 85 |
It *should* be okay as we make sure the image size is a multiple of 8 bytes however it is not |
| 86 |
guaranteed that sws_scale won't at some point require more bytes per line. |
| 87 |
**/ |
| 88 |
bool convertImage_sws(const QImage &image, QString* errorString = nullptr); |
| 89 |
}; |
| 90 |
|
| 91 |
#endif //Q_VIDEO_ENCODE_HEADER |