Added the initial API of the FFmpegWriter class, and tweaked the build scripts and reader class.

This commit is contained in:
Jonathan Thomas
2012-07-12 15:55:41 -05:00
parent 902b2ff8cd
commit 1e5cffd6db
11 changed files with 214 additions and 24 deletions

View File

@@ -74,6 +74,16 @@ namespace openshot {
virtual ~NoStreamsFound() throw () {}
};
/// Exception when no valid format is found for a file
class InvalidFormat : public BaseException
{
public:
string file_path;
InvalidFormat(string message, string file_path)
: BaseException(message), file_path(file_path) { }
virtual ~InvalidFormat() throw () {}
};
/// Exception when no valid codec is found for a file
class InvalidCodec : public BaseException
{

View File

@@ -1,5 +1,5 @@
#ifndef OPENSHOT_FFMPEG_READER_BASE_H
#define OPENSHOT_FFMPEG_READER_BASE_H
#ifndef OPENSHOT_FFMPEG_READER_H
#define OPENSHOT_FFMPEG_READER_H
/**
* \file
@@ -66,7 +66,6 @@ namespace openshot
bool check_interlace;
bool check_fps;
bool init_settings;
Cache final_cache;
Cache working_cache;

91
include/FFmpegWriter.h Normal file
View File

@@ -0,0 +1,91 @@
#ifndef OPENSHOT_FFMPEG_WRITER_H
#define OPENSHOT_FFMPEG_WRITER_H
/**
* \file
* \brief Header file for FFmpegWriter class
* \author Copyright (c) 2011 Jonathan Thomas
*/
#include "FileReaderBase.h"
#include "FileWriterBase.h"
// Required for libavformat to build on Windows
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#include <cmath>
#include <ctime>
#include <iostream>
#include <omp.h>
#include <stdio.h>
#include "Magick++.h"
#include "Cache.h"
#include "Exceptions.h"
using namespace std;
namespace openshot
{
/**
* This enumeration designates which a type of stream when encoding
*/
enum Stream_Type
{
VIDEO_STREAM,
AUDIO_STREAM
};
/**
* \brief This class uses the FFmpeg libraries, to write and encode video files and audio files
*
* TODO
*/
class FFmpegWriter : public FileWriterBase
{
private:
string path;
public:
/// Constructor for FFmpegWriter. Throws one of the following exceptions.
FFmpegWriter(string path) throw(InvalidFile, InvalidFormat, InvalidCodec);
/// Set video export options
void SetVideoOptions(string codec, Fraction fps, int width, int height, Fraction display_ratio,
Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate);
/// Set audio export options
void SetAudioOptions(string codec, int sample_rate, int channels, int bit_rate);
/// Set custom options (some codecs accept additional params)
void SetOption(Stream_Type stream, string name, double value);
/// Write the file header (after the options are set)
void WriteHeader();
/// Write a single frame
void WriteFrame(Frame frame);
/// Write a block of frames from a reader
void WriteFrame(FileReaderBase* reader, int start, int length);
/// Write the file trailer (after all frames are written)
void WriteTrailer();
/// Close the writer
void Close();
};
}
#endif

View File

@@ -23,7 +23,7 @@ namespace openshot
* about the streams. Derived classes of FileReaderBase should call the InitFileInfo() method to initialize the
* default values of this struct.
*/
struct FileInfo
struct ReaderInfo
{
bool has_video; ///< Determines if this file has a video stream
bool has_audio; ///< Determines if this file has an audio stream
@@ -61,7 +61,7 @@ namespace openshot
{
public:
/// Information about the current media file
FileInfo info;
ReaderInfo info;
/// This method is required for all derived classes of FileReaderBase, and return the
/// openshot::Frame object, which contains the image and audio information for that
@@ -71,8 +71,8 @@ namespace openshot
/// @param[in] number The frame number that is requested.
virtual Frame GetFrame(int number) = 0;
/// Initialize the values of the FileInfo struct. It is important for derived classes to call
/// this method, or the FileInfo struct values will not be initialized.
/// Initialize the values of the ReaderInfo struct. It is important for derived classes to call
/// this method, or the ReaderInfo struct values will not be initialized.
void InitFileInfo();
/// Display file information in the standard output stream (stdout)

View File

@@ -62,13 +62,12 @@ namespace openshot
/// Information about the current media file
WriterInfo info;
/// This method is required for all derived classes of FileReaderBase, and return the
/// openshot::Frame object, which contains the image and audio information for that
/// frame of video.
///
/// @returns The requested frame of video
/// @param[in] number The frame number that is requested.
virtual void WriteFrame(Frame frame);
/// This method is required for all derived classes of FileWriterBase
virtual void WriteFrame(Frame frame) = 0;
/// Initialize the values of the WriterInfo struct. It is important for derived classes to call
/// this method, or the WriterInfo struct values will not be initialized.
void InitFileInfo();
/// Display file information in the standard output stream (stdout)
void DisplayInfo();

View File

@@ -29,7 +29,9 @@
#include "Coordinate.h"
#include "Exceptions.h"
#include "FileReaderBase.h"
#include "FileWriterBase.h"
#include "FFmpegReader.h"
#include "FFmpegWriter.h"
#include "Fraction.h"
#include "Frame.h"
#include "FrameMapper.h"

View File

@@ -39,9 +39,10 @@ add_library(openshot SHARED
AudioBufferSource.cpp
Cache.cpp
Coordinate.cpp
FFmpegReader.cpp
FileReaderBase.cpp
FileWriterBase.cpp
FFmpegReader.cpp
FFmpegWriter.cpp
Fraction.cpp
Frame.cpp
FrameMapper.cpp

View File

@@ -5,8 +5,10 @@ using namespace openshot;
FFmpegReader::FFmpegReader(string path) throw(InvalidFile, NoStreamsFound, InvalidCodec)
: last_frame(0), is_seeking(0), seeking_pts(0), seeking_frame(0),
audio_pts_offset(99999), video_pts_offset(99999), working_cache(10), final_cache(10), path(path),
is_video_seek(true), check_interlace(false), check_fps(false), init_settings(false),
enable_seek(true) {
is_video_seek(true), check_interlace(false), check_fps(false), enable_seek(true) {
// Init FileInfo struct (clear all values)
InitFileInfo();
// Initialize FFMpeg, and register all formats and codecs
av_register_all();
@@ -52,13 +54,6 @@ void FFmpegReader::Open()
if (videoStream == -1 && audioStream == -1)
throw NoStreamsFound("No video or audio streams found in this file.", path);
// Init FileInfo struct (clear all values)
if (!init_settings)
{
InitFileInfo();
init_settings = true;
}
// Is there a video stream?
if (videoStream != -1)
{

61
src/FFmpegWriter.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include "../include/FFmpegWriter.h"
using namespace openshot;
FFmpegWriter::FFmpegWriter(string path) throw(InvalidFile, InvalidFormat, InvalidCodec)
{
// Init FileInfo struct (clear all values)
InitFileInfo();
// Initialize FFMpeg, and register all formats and codecs
av_register_all();
}
// Set video export options
void FFmpegWriter::SetVideoOptions(string codec, Fraction fps, int width, int height, Fraction display_ratio,
Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate)
{
}
// Set audio export options
void FFmpegWriter::SetAudioOptions(string codec, int sample_rate, int channels, int bit_rate)
{
}
// Set custom options (some codecs accept additional params)
void FFmpegWriter::SetOption(Stream_Type stream, string name, double value)
{
}
// Write the file header (after the options are set)
void FFmpegWriter::WriteHeader()
{
}
// Write a single frame
void FFmpegWriter::WriteFrame(Frame frame)
{
}
// Write a block of frames from a reader
void FFmpegWriter::WriteFrame(FileReaderBase* reader, int start, int length)
{
}
// Write the file trailer (after all frames are written)
void FFmpegWriter::WriteTrailer()
{
}
// Close the writer
void FFmpegWriter::Close()
{
}

View File

@@ -2,6 +2,34 @@
using namespace openshot;
// Initialize the values of the FileInfo struct
void FileWriterBase::InitFileInfo()
{
info.has_video = false;
info.has_audio = false;
info.duration = 0.0;
info.file_size = 0;
info.height = 0;
info.width = 0;
info.pixel_format = -1;
info.fps = Fraction();
info.video_bit_rate = 0;
info.pixel_ratio = Fraction();
info.display_ratio = Fraction();
info.vcodec = "";
info.video_length = 0;
info.video_stream_index = -1;
info.video_timebase = Fraction();
info.interlaced_frame = false;
info.top_field_first = true;
info.acodec = "";
info.audio_bit_rate = 0;
info.sample_rate = 0;
info.channels = 0;
info.audio_stream_index = -1;
info.audio_timebase = Fraction();
}
// Display file information
void FileWriterBase::DisplayInfo() {
cout << fixed << setprecision(2) << boolalpha;

View File

@@ -9,7 +9,9 @@
#include "../include/Coordinate.h"
#include "../include/Exceptions.h"
#include "../include/FileReaderBase.h"
#include "../include/FileWriterBase.h"
#include "../include/FFmpegReader.h"
#include "../include/FFmpegWriter.h"
#include "../include/Fraction.h"
#include "../include/Frame.h"
#include "../include/FrameMapper.h"
@@ -89,7 +91,9 @@
%include "../include/Coordinate.h"
%include "../include/Exceptions.h"
%include "../include/FileReaderBase.h"
%include "../include/FileWriterBase.h"
%include "../include/FFmpegReader.h"
%include "../include/FFmpegWriter.h"
%include "../include/Fraction.h"
%include "../include/Frame.h"
%include "../include/FrameMapper.h"