diff --git a/include/Exceptions.h b/include/Exceptions.h index e877bfa1..59812493 100644 --- a/include/Exceptions.h +++ b/include/Exceptions.h @@ -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 { diff --git a/include/FFmpegReader.h b/include/FFmpegReader.h index 3c5881b9..9ffc1c5e 100644 --- a/include/FFmpegReader.h +++ b/include/FFmpegReader.h @@ -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; diff --git a/include/FFmpegWriter.h b/include/FFmpegWriter.h new file mode 100644 index 00000000..fa39a816 --- /dev/null +++ b/include/FFmpegWriter.h @@ -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 + #include + #include +} +#include +#include +#include +#include +#include +#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 diff --git a/include/FileReaderBase.h b/include/FileReaderBase.h index 250b44f2..0b54e0f2 100644 --- a/include/FileReaderBase.h +++ b/include/FileReaderBase.h @@ -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) diff --git a/include/FileWriterBase.h b/include/FileWriterBase.h index 7d795be7..ab6222da 100644 --- a/include/FileWriterBase.h +++ b/include/FileWriterBase.h @@ -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(); diff --git a/include/OpenShot.h b/include/OpenShot.h index 83d4c610..4520c46d 100644 --- a/include/OpenShot.h +++ b/include/OpenShot.h @@ -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" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea850e9d..faf5ef60 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index bda1338b..161fec18 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -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) { diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp new file mode 100644 index 00000000..60b9b0ba --- /dev/null +++ b/src/FFmpegWriter.cpp @@ -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() +{ + +} diff --git a/src/FileWriterBase.cpp b/src/FileWriterBase.cpp index dd442cb1..2088d38c 100644 --- a/src/FileWriterBase.cpp +++ b/src/FileWriterBase.cpp @@ -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; diff --git a/src/openshot.i b/src/openshot.i index 35a6f24d..4ef564b3 100644 --- a/src/openshot.i +++ b/src/openshot.i @@ -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"