From da3bc860f66d34f233028e819beee32dd1c43dd8 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 18 Jan 2018 16:49:54 -0500 Subject: Modernized project This rewrite brings the codebase of this project more in line with the format of the other bots, including things like C++ randomization, better abstraction, use of exceptions, etc. Notably, any FFMPEG objects that get allocated are wrapped in simple objects so that they get properly destroyed if an exception is thrown. Some more error detection and cleanliness stuff can probably be done but my wrists really hurt. Also updated librawr, and thus also removed the yaml-cpp submodule. --- .gitignore | 5 + CMakeLists.txt | 28 ++- designer.cpp | 244 ++++++++++++++++++++++++++ designer.h | 40 +++++ director.cpp | 378 ++++++++++++++++++++++++++++++++++++++++ director.h | 31 ++++ main.cpp | 45 +++++ sap.cpp | 492 ++++++++--------------------------------------------- sap.h | 34 ++++ util.h | 60 +++++++ vendor/rawr-ebooks | 2 +- 11 files changed, 927 insertions(+), 432 deletions(-) create mode 100644 .gitignore create mode 100644 designer.cpp create mode 100644 designer.h create mode 100644 director.cpp create mode 100644 director.h create mode 100644 main.cpp create mode 100644 sap.h create mode 100644 util.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e2b3f04 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +CMakeFiles +CMakeCache.txt +cmake_install.cmake +Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt index 70a9572..efafdaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,22 +1,34 @@ cmake_minimum_required (VERSION 3.1) project (sap) -set(CMAKE_BUILD_TYPE Debug) - set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") find_package(FFMPEG REQUIRED) -include_directories(${FFMPEG_INCLUDE_DIRS}) find_package(PkgConfig) +pkg_check_modules(yaml-cpp yaml-cpp REQUIRED) pkg_check_modules(GraphicsMagick GraphicsMagick++ REQUIRED) -include_directories(${GraphicsMagick_INCLUDE_DIRS}) -link_directories(${GraphicsMagick_LIBRARY_DIRS}) add_subdirectory(vendor/rawr-ebooks EXCLUDE_FROM_ALL) -include_directories(vendor/rawr-ebooks vendor/rawr-ebooks/vendor/libtwittercpp/src vendor/rawr-ebooks/vendor/yaml-cpp/include) -add_executable(sap sap.cpp) +include_directories( + ${FFMPEG_INCLUDE_DIRS} + vendor/rawr-ebooks + vendor/rawr-ebooks/vendor/libtwittercpp/src + ${yaml-cpp_INCLUDE_DIRS} + ${GraphicsMagick_INCLUDE_DIRS}) + +link_directories( + ${yaml-cpp_LIBRARY_DIRS} + ${GraphicsMagick_LIBRARY_DIRS}) + +add_executable(sap sap.cpp director.cpp designer.cpp main.cpp) set_property(TARGET sap PROPERTY CXX_STANDARD 11) set_property(TARGET sap PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(sap rawr twitter++ ${FFMPEG_LIBRARIES} ${GraphicsMagick_LIBRARIES} yaml-cpp) + +target_link_libraries(sap + rawr + twitter++ + ${FFMPEG_LIBRARIES} + ${GraphicsMagick_LIBRARIES} + ${yaml-cpp_LIBRARIES}) diff --git a/designer.cpp b/designer.cpp new file mode 100644 index 0000000..3bdcbeb --- /dev/null +++ b/designer.cpp @@ -0,0 +1,244 @@ +#include "designer.h" +#include +#include +#include +#include "util.h" + +designer::designer(std::string fontsDir) +{ + DIR* fontdir; + struct dirent* ent; + if ((fontdir = opendir(fontsDir.c_str())) == nullptr) + { + throw std::invalid_argument("Couldn't find fonts"); + } + + while ((ent = readdir(fontdir)) != nullptr) + { + std::string dname(ent->d_name); + if ((dname.find(".otf") != std::string::npos) + || (dname.find(".ttf") != std::string::npos)) + { + fonts.push_back(fontsDir + "/" + dname); + } + } + + closedir(fontdir); +} + +Magick::Image designer::generate( + size_t width, + size_t height, + const std::string& text, + std::mt19937& rng) const +{ + // Initialize two layers: the text, and a shadow to increase contrast. + Magick::Image textimage(Magick::Geometry(width, height), "transparent"); + Magick::Image shadowimage(Magick::Geometry(width, height), "transparent"); + + textimage.fillColor(Magick::Color(MaxRGB, MaxRGB, MaxRGB, MaxRGB * 0.0)); + shadowimage.fillColor(Magick::Color(0, 0, 0, 0)); + shadowimage.strokeColor("black"); + + bool hasFont = false; + std::uniform_int_distribution fontDist(0, fonts.size() - 1); + + std::vector words = split>(text, " "); + std::vector::const_iterator curWord = std::begin(words); + + size_t top = V_PADDING; + size_t minWords = 1; + while (curWord != std::end(words)) + { + // There is a 1 in 10 chance of randomly changing the font. We also have to + // set the font if this is the beginning of generation. + if (!hasFont || (std::bernoulli_distribution(0.1)(rng))) + { + std::string font = fonts[fontDist(rng)]; + + textimage.font(font); + shadowimage.font(font); + + hasFont = true; + } + + // Choose a font size for the current line. + std::uniform_int_distribution sizeDist( + MIN_FONT_SIZE, MAX_FONT_SIZE); + + size_t size = sizeDist(rng); + textimage.fontPointsize(size); + + // Decide what words to put on this line. + size_t maxWords = maxWordsInLine(curWord, std::end(words), textimage); + + size_t lineLen; + if (minWords > maxWords) + { + lineLen = maxWords; + } else { + std::uniform_int_distribution lenDist(minWords, maxWords); + lineLen = lenDist(rng); + } + + std::string prefixText = implode(curWord, curWord + lineLen, " "); + + // Determine if the choice of font, font size, and number of words, would + // prevent the algorithm from being about to layout the entire string even + // if the rest of it were rendered in the smallest font size. + Magick::TypeMetric metric; + textimage.fontTypeMetrics(prefixText, &metric); + + textimage.fontPointsize(MIN_FONT_SIZE); + + // This is how much vertical space would be required to render the rest of + // the string at the minimum font size. + int lowpadding = minHeightRequired( + curWord + lineLen, + std::end(words), + textimage); + + // This is the amount of space that would be left over if the rest of the + // string were rendered at the minimum font size. + int freespace = + static_cast(height) - static_cast(V_PADDING) + - static_cast(top) - lowpadding - metric.textHeight(); + + // Some debug text. + std::cout << "top of " << top << " with lowpad of " << lowpadding + << " and textheight of " << metric.textHeight() << " with freespace=" + << freespace << std::endl; + + // If there wouldn't be enough room to render the rest of the string at the + // minimum font size, go back to the top of the loop and choose a different + // font, font size, and number of words. + if (freespace < 0) + { + minWords = lineLen; + + continue; + } + + minWords = 1; + + // Determine how much space to leave between this line and the previous one. + size_t toppadding; + if (std::bernoulli_distribution(0.5)(rng)) + { + // Exponential distribution, biased toward top + std::uniform_int_distribution expDist( + 1, static_cast(exp(freespace + 1))); + + toppadding = log(expDist(rng)); + } else { + // Linear distribution, biased toward bottom + std::uniform_int_distribution linDist(0, freespace); + + toppadding = linDist(rng); + } + + // Determine the x-coordinate of this line. + std::uniform_int_distribution leftDist( + H_PADDING, + width - H_PADDING - static_cast(metric.textWidth()) - 1); + + size_t leftx = leftDist(rng); + + // Render this line. + size_t ycor = top + toppadding + metric.ascent(); + + std::cout << "printing at " << leftx << "," << ycor << std::endl; + + textimage.fontPointsize(size); + textimage.annotate(prefixText, Magick::Geometry(0, 0, leftx, ycor)); + + shadowimage.fontPointsize(size); + shadowimage.strokeWidth(size / 10); + shadowimage.annotate(prefixText, Magick::Geometry(0, 0, leftx, ycor)); + + top += toppadding + metric.textHeight(); + + std::advance(curWord, lineLen); + } + + // Make the shadow layer semi-transparent. + Magick::PixelPacket* shadpixels = shadowimage.getPixels(0, 0, width, height); + Magick::PixelPacket* textpixels = textimage.getPixels(0, 0, width, height); + for (size_t j = 0; j < height; j++) + { + for (size_t i = 0; i < width; i++) + { + size_t ind = j * width + i; + + if (shadpixels[ind].opacity != MaxRGB) + { + shadpixels[ind].opacity = MaxRGB * 0.25; + } + } + } + + shadowimage.syncPixels(); + textimage.syncPixels(); + + // Add some blur to the text and shadow. + shadowimage.blur(10.0, 20.0); + textimage.blur(0.0, 0.5); + + // Put the text layer on top of the shadow. + shadowimage.composite(textimage, 0, 0, Magick::OverCompositeOp); + + return shadowimage; +} + +size_t designer::maxWordsInLine( + std::vector::const_iterator first, + std::vector::const_iterator last, + Magick::Image& textimage) const +{ + size_t result = 0; + + std::string curline = ""; + for (; first != last; first++) + { + curline += " " + *first; + + Magick::TypeMetric metric; + textimage.fontTypeMetrics(curline, &metric); + + if (metric.textWidth() > ((textimage.columns() / 10) * 9)) + { + break; + } else { + result++; + } + } + + return result; +} + +size_t designer::minHeightRequired( + std::vector::const_iterator first, + std::vector::const_iterator last, + Magick::Image& textimage) const +{ + if (first == last) + { + return 0; + } else { + size_t result = 0; + + while (first != last) + { + size_t prefixlen = maxWordsInLine(first, last, textimage); + std::string prefixText = implode(first, first + prefixlen, " "); + + Magick::TypeMetric metric; + textimage.fontTypeMetrics(prefixText, &metric); + result += metric.textHeight() + V_PADDING; + + std::advance(first, prefixlen); + } + + return result - V_PADDING; + } +} diff --git a/designer.h b/designer.h new file mode 100644 index 0000000..6a348a0 --- /dev/null +++ b/designer.h @@ -0,0 +1,40 @@ +#ifndef DESIGNER_H_CCE34BEB +#define DESIGNER_H_CCE34BEB + +#include +#include +#include +#include + +class designer { +public: + + designer(std::string fontsPath); + + Magick::Image generate( + size_t width, + size_t height, + const std::string& text, + std::mt19937& rng) const; + +private: + + const size_t MIN_FONT_SIZE = 48; + const size_t MAX_FONT_SIZE = 96; + const size_t V_PADDING = 5; + const size_t H_PADDING = 5; + + size_t maxWordsInLine( + std::vector::const_iterator first, + std::vector::const_iterator last, + Magick::Image& textimage) const; + + size_t minHeightRequired( + std::vector::const_iterator first, + std::vector::const_iterator last, + Magick::Image& textimage) const; + + std::vector fonts; +}; + +#endif /* end of include guard: DESIGNER_H_CCE34BEB */ diff --git a/director.cpp b/director.cpp new file mode 100644 index 0000000..24335da --- /dev/null +++ b/director.cpp @@ -0,0 +1,378 @@ +#include "director.h" +#include +#include + +extern "C" { +#include +#include +#include +#include +} + +namespace ffmpeg { + + class format { + public: + + format(std::string videoPath) + { + if (avformat_open_input(&ptr_, videoPath.c_str(), nullptr, nullptr)) + { + throw ffmpeg_error("Could not open video " + videoPath); + } + + if (avformat_find_stream_info(ptr_, nullptr)) + { + throw ffmpeg_error("Could not read stream"); + } + } + + ~format() + { + avformat_close_input(&ptr_); + } + + format(const format& other) = delete; + + AVFormatContext* ptr() + { + return ptr_; + } + + private: + + AVFormatContext* ptr_ = nullptr; + }; + + class codec { + public: + + codec( + format& fmt, + AVStream* st) + { + AVCodec* dec = avcodec_find_decoder(st->codecpar->codec_id); + if (!dec) + { + throw ffmpeg_error("Failed to find codec"); + } + + ptr_ = avcodec_alloc_context3(dec); + if (!ptr_) + { + throw ffmpeg_error("Failed to allocate codec context"); + } + + if (avcodec_parameters_to_context(ptr_, st->codecpar) < 0) + { + throw ffmpeg_error("Failed to copy codec parameters to decoder"); + } + + // Init the decoders, with or without reference counting + AVDictionary* opts = nullptr; + av_dict_set(&opts, "refcounted_frames", "0", 0); + + if (avcodec_open2(ptr_, dec, &opts) < 0) + { + throw ffmpeg_error("Failed to open codec"); + } + } + + codec(const codec& other) = delete; + + ~codec() + { + avcodec_free_context(&ptr_); + } + + int getWidth() const + { + return ptr_->width; + } + + int getHeight() const + { + return ptr_->height; + } + + enum AVPixelFormat getPixelFormat() const + { + return ptr_->pix_fmt; + } + + AVCodecContext* ptr() + { + return ptr_; + } + + private: + + AVCodecContext* ptr_ = nullptr; + }; + + class packet { + public: + + packet() + { + ptr_ = av_packet_alloc(); + } + + ~packet() + { + av_packet_free(&ptr_); + } + + packet(const packet& other) = delete; + + int getStreamIndex() const + { + return ptr_->stream_index; + } + + AVPacket* ptr() + { + return ptr_; + } + + int getFlags() const + { + return ptr_->flags; + } + + private: + + AVPacket* ptr_ = nullptr; + }; + + class frame { + public: + + frame() + { + ptr_ = av_frame_alloc(); + } + + ~frame() + { + av_frame_free(&ptr_); + } + + frame(const frame& other) = delete; + + uint8_t** getData() + { + return ptr_->data; + } + + int* getLinesize() + { + return ptr_->linesize; + } + + AVFrame* ptr() + { + return ptr_; + } + + private: + + AVFrame* ptr_ = nullptr; + }; + + class sws { + public: + + sws( + int srcW, + int srcH, + enum AVPixelFormat srcFormat, + int dstW, + int dstH, + enum AVPixelFormat dstFormat, + int flags, + SwsFilter* srcFilter, + SwsFilter* dstFilter, + const double* param) + { + ptr_ = sws_getContext( + srcW, + srcH, + srcFormat, + dstW, + dstH, + dstFormat, + flags, + srcFilter, + dstFilter, + param); + + if (ptr_ == NULL) + { + throw ffmpeg_error("Could not allocate sws context"); + } + } + + ~sws() + { + sws_freeContext(ptr_); + } + + sws(const sws& other) = delete; + + void scale( + const uint8_t* const srcSlice[], + const int srcStride[], + int srcSliceY, + int srcSliceH, + uint8_t* const dst[], + const int dstStride[]) + { + sws_scale( + ptr_, + srcSlice, + srcStride, + srcSliceY, + srcSliceH, + dst, + dstStride); + } + + private: + + SwsContext* ptr_; + }; + +} + +director::director(std::string videosPath) : videosPath_(videosPath) +{ + DIR* videodir; + struct dirent* ent; + if ((videodir = opendir(videosPath.c_str())) == nullptr) + { + throw std::invalid_argument("Couldn't find videos"); + } + + while ((ent = readdir(videodir)) != nullptr) + { + std::string dname(ent->d_name); + if (dname.find(".mp4") != std::string::npos) + { + videos_.push_back(dname); + } + } + + closedir(videodir); +} + +Magick::Image director::generate(std::mt19937& rng) const +{ + std::uniform_int_distribution videoDist(0, videos_.size() - 1); + std::string video = videosPath_ + "/" + videos_[videoDist(rng)]; + + ffmpeg::format fmt(video); + + int streamIdx = + av_find_best_stream(fmt.ptr(), AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); + + if (streamIdx < 0) + { + throw ffmpeg_error("Could not find stream"); + } + + AVStream* stream = fmt.ptr()->streams[streamIdx]; + + ffmpeg::codec cdc(fmt, stream); + + size_t codecw = cdc.getWidth(); + size_t codech = cdc.getHeight(); + + std::uniform_int_distribution frameDist(0, stream->duration - 1); + int64_t seek = frameDist(rng); + + std::cout << seek << std::endl; + + if (av_seek_frame(fmt.ptr(), streamIdx, seek, 0)) + { + throw ffmpeg_error("Could not seek"); + } + + ffmpeg::frame frame; + ffmpeg::frame converted; + + av_image_alloc( + converted.getData(), + converted.getLinesize(), + codecw, + codech, + AV_PIX_FMT_RGB24, + 1); + + ffmpeg::packet pkt; + + do + { + if (av_read_frame(fmt.ptr(), pkt.ptr()) < 0) + { + throw ffmpeg_error("Could not read frame"); + } + } while ((pkt.getStreamIndex() != streamIdx) + || !(pkt.getFlags() & AV_PKT_FLAG_KEY)); + + int ret; + do { + if (avcodec_send_packet(cdc.ptr(), pkt.ptr()) < 0) + { + throw ffmpeg_error("Could not send packet"); + } + + ret = avcodec_receive_frame(cdc.ptr(), frame.ptr()); + } while (ret == AVERROR(EAGAIN)); + + if (ret < 0) + { + throw ffmpeg_error("Could not decode frame"); + } + + ffmpeg::sws scaler( + cdc.getWidth(), + cdc.getHeight(), + cdc.getPixelFormat(), + cdc.getWidth(), + cdc.getHeight(), + AV_PIX_FMT_RGB24, + 0, nullptr, nullptr, 0); + + scaler.scale( + frame.getData(), + frame.getLinesize(), + 0, + codech, + converted.getData(), + converted.getLinesize()); + + size_t buffer_size = av_image_get_buffer_size( + AV_PIX_FMT_RGB24, cdc.getWidth(), cdc.getHeight(), 1); + + std::vector buffer(buffer_size); + + av_image_copy_to_buffer( + buffer.data(), + buffer_size, + converted.getData(), + converted.getLinesize(), + AV_PIX_FMT_RGB24, + cdc.getWidth(), + cdc.getHeight(), + 1); + + size_t width = 1024; + size_t height = codech * width / codecw; + + Magick::Image image; + image.read(codecw, codech, "RGB", Magick::CharPixel, buffer.data()); + image.zoom(Magick::Geometry(width, height)); + + return image; +} diff --git a/director.h b/director.h new file mode 100644 index 0000000..f00f2ee --- /dev/null +++ b/director.h @@ -0,0 +1,31 @@ +#ifndef DIRECTOR_H_9DFD929C +#define DIRECTOR_H_9DFD929C + +#include +#include +#include +#include +#include + +class ffmpeg_error : public std::runtime_error { +public: + + ffmpeg_error(std::string msg) : std::runtime_error(msg) + { + } +}; + +class director { +public: + + director(std::string videosPath); + + Magick::Image generate(std::mt19937& rng) const; + +private: + + std::string videosPath_; + std::vector videos_; +}; + +#endif /* end of include guard: DIRECTOR_H_9DFD929C */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6ac8ee9 --- /dev/null +++ b/main.cpp @@ -0,0 +1,45 @@ +#include "sap.h" +#include +#include +#include + +extern "C" { +#include +} + +int main(int argc, char** argv) +{ + Magick::InitializeMagick(nullptr); + av_register_all(); + + std::random_device randomDevice; + std::mt19937 rng(randomDevice()); + + // We also have to seed the C-style RNG because librawr uses it. + srand(time(NULL)); + rand(); rand(); rand(); rand(); + + if (argc != 2) + { + std::cout << "usage: sap [configfile]" << std::endl; + return -1; + } + + std::string configfile(argv[1]); + + try + { + sap bot(configfile, rng); + + try + { + bot.run(); + } catch (const std::exception& ex) + { + std::cout << "Error running bot: " << ex.what() << std::endl; + } + } catch (const std::exception& ex) + { + std::cout << "Error initializing bot: " << ex.what() << std::endl; + } +} diff --git a/sap.cpp b/sap.cpp index 7e1412a..f0c3fd1 100644 --- a/sap.cpp +++ b/sap.cpp @@ -1,22 +1,9 @@ -extern "C" { -#include -#include -#include -#include -} - -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "sap.h" #include #include #include +#include +#include /* - random frames from Spongebob (using ffmpeg) * with strange text overlaid, possibly rawr'd from @@ -25,277 +12,24 @@ extern "C" { * frames */ -template -Container split(std::string input, std::string delimiter) -{ - Container result; - - while (!input.empty()) - { - int divider = input.find(delimiter); - if (divider == std::string::npos) - { - result.push_back(input); - - input = ""; - } else { - result.push_back(input.substr(0, divider)); - - input = input.substr(divider+delimiter.length()); - } - } - - return result; -} - -template -std::string implode(InputIterator first, InputIterator last, std::string delimiter) -{ - std::stringstream result; - - for (InputIterator it = first; it != last; it++) - { - if (it != first) - { - result << delimiter; - } - - result << *it; - } - - return result.str(); -} - -int maxWordsInLine(std::vector words, Magick::Image& textimage) -{ - int result = 0; - - std::string curline = ""; - Magick::TypeMetric metric; - for (auto word : words) - { - curline += " " + word; - - textimage.fontTypeMetrics(curline, &metric); - if (metric.textWidth() > ((textimage.columns()/10)*9)) - { - break; - } else { - result++; - } - } - - return result; -} - -int minHeightRequired(std::vector words, Magick::Image& textimage) -{ - int result = 0; - while (!words.empty()) - { - int prefixlen = maxWordsInLine(words, textimage); - std::string prefixText = implode(std::begin(words), std::begin(words) + prefixlen, " "); - std::vector suffix(std::begin(words) + prefixlen, std::end(words)); - Magick::TypeMetric metric; - textimage.fontTypeMetrics(prefixText, &metric); - result += metric.textHeight() + 5; - - words = suffix; - } - - return result - 5; -} - -void layoutText(Magick::Image& textimage, Magick::Image& shadowimage, int width, int height, std::string text, const std::vector& fonts) -{ - textimage.fillColor(Magick::Color(MaxRGB, MaxRGB, MaxRGB, MaxRGB * 0.0)); - shadowimage.fillColor(Magick::Color(0, 0, 0, 0)); - shadowimage.strokeColor("black"); - - int minSize = 48; - int realMaxSize = 96; - int maxSize = realMaxSize; - Magick::TypeMetric metric; - std::string font; - auto words = split>(text, " "); - int top = 5; - int minWords = 1; - while (!words.empty()) - { - if (font.empty() || (rand() % 10 == 0)) - { - font = fonts[rand() % fonts.size()]; - textimage.font(font); - shadowimage.font(font); - } - - int size = rand() % (maxSize - minSize + 1) + minSize; - textimage.fontPointsize(size); - int maxWords = maxWordsInLine(words, textimage); - int touse; - if (minWords > maxWords) - { - touse = maxWords; - } else { - touse = rand() % (maxWords - minWords + 1) + minWords; - } - std::string prefixText = implode(std::begin(words), std::begin(words) + touse, " "); - std::vector suffix(std::begin(words) + touse, std::end(words)); - textimage.fontTypeMetrics(prefixText, &metric); - - textimage.fontPointsize(minSize); - int lowpadding = minHeightRequired(suffix, textimage); - int freespace = height - 5 - top - lowpadding - metric.textHeight(); - std::cout << "top of " << top << " with lowpad of " << lowpadding << " and textheight of " << metric.textHeight() << " with freespace=" << freespace << std::endl; - if (freespace < 0) - { - minWords = touse; - - continue; - } - - maxSize = realMaxSize; - minWords = 1; - - int toppadding; - if (rand() % 2 == 0) - { - // Exponential distribution, biased toward top - toppadding = log(rand() % (int)exp(freespace + 1) + 1); - } else { - // Linear distribution, biased toward bottom - toppadding = rand() % (freespace + 1); - } - - int leftx = rand() % (width - 10 - (int)metric.textWidth()) + 5; - std::cout << "printing at " << leftx << "," << (top + toppadding + metric.ascent()) << std::endl; - textimage.fontPointsize(size); - textimage.annotate(prefixText, Magick::Geometry(0, 0, leftx, top + toppadding + metric.ascent())); - - shadowimage.fontPointsize(size); - shadowimage.strokeWidth(size / 10); - shadowimage.annotate(prefixText, Magick::Geometry(0, 0, leftx, top + toppadding + metric.ascent())); - //shadowimage.draw(Magick::DrawableRectangle(leftx - 5, top + toppadding, leftx + metric.textWidth() + 5, top + toppadding + metric.textHeight() + 10 + metric.descent())); - - words = suffix; - top += toppadding + metric.textHeight(); - } - - Magick::PixelPacket* shadowpixels = shadowimage.getPixels(0, 0, width, height); - Magick::PixelPacket* textpixels = textimage.getPixels(0, 0, width, height); - for (int j=0; jstreams[stream_index]; - - // find decoder for the stream - dec_ctx = st->codec; - dec = avcodec_find_decoder(dec_ctx->codec_id); - if (!dec) - { - fprintf(stderr, "Failed to find %s codec\n", av_get_media_type_string(type)); - return AVERROR(EINVAL); - } - - // Init the decoders, with or without reference counting - av_dict_set(&opts, "refcounted_frames", "0", 0); - if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) - { - fprintf(stderr, "Failed to open %s codec\n", av_get_media_type_string(type)); - return ret; - } - - *stream_idx = stream_index; - } - - return 0; -} - -int main(int argc, char** argv) +sap::sap( + std::string configFile, + std::mt19937& rng) : + rng_(rng) { - srand(time(NULL)); - rand(); rand(); rand(); rand(); - - Magick::InitializeMagick(nullptr); - av_register_all(); + // Load the config file. + YAML::Node config = YAML::LoadFile(configFile); - if (argc != 2) - { - std::cout << "usage: sap [configfile]" << std::endl; - return -1; - } - - std::string configfile(argv[1]); - YAML::Node config = YAML::LoadFile(configfile); - + // Set up the Twitter client. twitter::auth auth; auth.setConsumerKey(config["consumer_key"].as()); auth.setConsumerSecret(config["consumer_secret"].as()); auth.setAccessKey(config["access_key"].as()); auth.setAccessSecret(config["access_secret"].as()); - - twitter::client client(auth); - - // Fonts - std::vector fonts; - { - std::string fontdirname = config["fonts"].as(); - DIR* fontdir; - struct dirent* ent; - if ((fontdir = opendir(fontdirname.c_str())) == nullptr) - { - std::cout << "Couldn't find fonts." << std::endl; - return -2; - } - while ((ent = readdir(fontdir)) != nullptr) - { - std::string dname(ent->d_name); - if ((dname.find(".otf") != std::string::npos) || (dname.find(".ttf") != std::string::npos)) - { - fonts.push_back(fontdirname + "/" + dname); - } - } + client_ = std::unique_ptr(new twitter::client(auth)); - closedir(fontdir); - } - - rawr kgramstats; + // Set up the text generator. for (const YAML::Node& corpusname : config["corpuses"]) { std::ifstream infile(corpusname.as()); @@ -307,164 +41,76 @@ int main(int argc, char** argv) { line.pop_back(); } - + corpus += line + " "; } - - kgramstats.addCorpus(corpus); - } - kgramstats.compile(5); - kgramstats.setMinCorpora(config["corpuses"].size()); - - std::string videodirname = config["videos"].as(); - DIR* videodir; - struct dirent* ent; - if ((videodir = opendir(videodirname.c_str())) == nullptr) - { - std::cout << "Couldn't find videos." << std::endl; - return -1; + kgramstats_.addCorpus(corpus); } - std::vector videos; - while ((ent = readdir(videodir)) != nullptr) - { - std::string dname(ent->d_name); - if (dname.find(".mp4") != std::string::npos) - { - videos.push_back(dname); - } - } + kgramstats_.compile(5); + kgramstats_.setMinCorpora(config["corpuses"].size()); - closedir(videodir); - + // Set up the layout designer. + layout_ = std::unique_ptr( + new designer(config["fonts"].as())); + + // Set up the frame picker. + director_ = std::unique_ptr( + new director(config["videos"].as())); +} + +void sap::run() const +{ for (;;) { - std::string video = videodirname + "/" + videos[rand() % videos.size()]; - std::cout << "Opening " << video << std::endl; - - AVFormatContext* format = nullptr; - if (avformat_open_input(&format, video.c_str(), nullptr, nullptr)) - { - std::cout << "could not open file" << std::endl; - return 1; - } - - if (avformat_find_stream_info(format, nullptr)) - { - std::cout << "could not read stream" << std::endl; - return 5; - } - - int video_stream_idx = -1; - if (open_codec_context(&video_stream_idx, format, AVMEDIA_TYPE_VIDEO)) - { - std::cout << "could not open codec" << std::endl; - return 6; - } - - AVStream* stream = format->streams[video_stream_idx]; - AVCodecContext* codec = stream->codec; - int codecw = codec->width; - int codech = codec->height; + std::cout << "Generating tweet..." << std::endl; - int64_t seek = (rand() % format->duration) * codec->time_base.num / codec->time_base.den; - std::cout << seek << std::endl; - if (av_seek_frame(format, video_stream_idx, seek, 0)) + try { - std::cout << "could not seek" << std::endl; - return 4; - } - - AVPacket packet; - av_init_packet(&packet); - - AVFrame* frame = av_frame_alloc(); - AVFrame* converted = av_frame_alloc(); - - int buffer_size = av_image_get_buffer_size(AV_PIX_FMT_RGB24, codecw, codech, 1); - uint8_t* buffer = new uint8_t[buffer_size]; + // Pick the video frame. + Magick::Image image = director_->generate(rng_); + + // Generate the text. + std::uniform_int_distribution lenDist(5, 19); + std::string action = kgramstats_.randomSentence(lenDist(rng_)); - av_image_alloc(converted->data, converted->linesize, codecw, codech, AV_PIX_FMT_RGB24, 1); + // Lay the text on the video frame. + Magick::Image textimage = + layout_->generate(image.columns(), image.rows(), action, rng_); + image.composite(textimage, 0, 0, Magick::OverCompositeOp); - for (;;) + // Send the tweet. + std::cout << "Sending tweet..." << std::endl; + + sendTweet(std::move(image)); + + std::cout << "Tweeted!" << std::endl; + + // Wait. + std::this_thread::sleep_for(std::chrono::hours(1)); + } catch (const Magick::ErrorImage& ex) { - if (av_read_frame(format, &packet)) - { - std::cout << "could not read frame" << std::endl; - return 2; - } - - if (packet.stream_index != video_stream_idx) - { - continue; - } - - int got_pic; - if (avcodec_decode_video2(codec, frame, &got_pic, &packet) < 0) - { - std::cout << "could not decode frame" << std::endl; - return 7; - } - - if (!got_pic) - { - continue; - } - - if (packet.flags && AV_PKT_FLAG_KEY) - { - SwsContext* sws = sws_getContext(codecw, codech, codec->pix_fmt, codecw, codech, AV_PIX_FMT_RGB24, 0, nullptr, nullptr, 0); - sws_scale(sws, frame->data, frame->linesize, 0, codech, converted->data, converted->linesize); - sws_freeContext(sws); - - av_image_copy_to_buffer(buffer, buffer_size, converted->data, converted->linesize, AV_PIX_FMT_RGB24, codecw, codech, 1); - av_frame_free(&frame); - av_frame_free(&converted); - av_packet_unref(&packet); - avcodec_close(codec); - avformat_close_input(&format); - - int width = 1024; - int height = codech * width / codecw; - - Magick::Image image; - image.read(codecw, codech, "RGB", Magick::CharPixel, buffer); - image.zoom(Magick::Geometry(width, height)); - - std::string action = kgramstats.randomSentence(rand() % 15 + 5); - Magick::Image textimage(Magick::Geometry(width, height), "transparent"); - Magick::Image shadowimage(Magick::Geometry(width, height), "transparent"); - layoutText(textimage, shadowimage, width, height, action, fonts); - image.composite(shadowimage, 0, 0, Magick::OverCompositeOp); - image.composite(textimage, 0, 0, Magick::OverCompositeOp); - - image.magick("jpeg"); - - Magick::Blob outputimg; - image.write(&outputimg); - - delete[] buffer; - - std::cout << "Generated image." << std::endl << "Tweeting..." << std::endl; - - try - { - long media_id = client.uploadMedia("image/jpeg", (const char*) outputimg.data(), outputimg.length()); - client.updateStatus("", {media_id}); - - std::cout << "Done!" << std::endl << "Waiting..." << std::endl << std::endl; - } catch (const twitter::twitter_error& error) - { - std::cout << "Twitter error: " << error.what() << std::endl; - } - - break; - } + std::cout << "Image error: " << ex.what() << std::endl; + } catch (const twitter::twitter_error& ex) + { + std::cout << "Twitter error: " << ex.what() << std::endl; + + std::this_thread::sleep_for(std::chrono::hours(1)); } - - std::this_thread::sleep_for(std::chrono::hours(1)); + + std::cout << std::endl; } - - return 0; +} + +void sap::sendTweet(Magick::Image image) const +{ + Magick::Blob outputimg; + image.magick("jpeg"); + image.write(&outputimg); + + long media_id = client_->uploadMedia("image/jpeg", + static_cast(outputimg.data()), outputimg.length()); + + client_->updateStatus("", {media_id}); } diff --git a/sap.h b/sap.h new file mode 100644 index 0000000..5288e57 --- /dev/null +++ b/sap.h @@ -0,0 +1,34 @@ +#ifndef SAP_H_11D8D668 +#define SAP_H_11D8D668 + +#include +#include +#include +#include +#include +#include +#include "designer.h" +#include "director.h" + +class sap { +public: + + sap( + std::string configFile, + std::mt19937& rng); + + void run() const; + +private: + + void sendTweet(Magick::Image image) const; + + std::mt19937& rng_; + std::unique_ptr client_; + rawr kgramstats_; + std::unique_ptr layout_; + std::unique_ptr director_; + +}; + +#endif /* end of include guard: SAP_H_11D8D668 */ diff --git a/util.h b/util.h new file mode 100644 index 0000000..c38f624 --- /dev/null +++ b/util.h @@ -0,0 +1,60 @@ +#ifndef UTIL_H_1F6A84F6 +#define UTIL_H_1F6A84F6 + +#include +#include +#include + +template +std::string implode( + InputIterator first, + InputIterator last, + std::string delimiter) +{ + std::stringstream result; + + for (InputIterator it = first; it != last; it++) + { + if (it != first) + { + result << delimiter; + } + + result << *it; + } + + return result.str(); +} + +template +void split(std::string input, std::string delimiter, OutputIterator out) +{ + while (!input.empty()) + { + int divider = input.find(delimiter); + if (divider == std::string::npos) + { + *out = input; + out++; + + input = ""; + } else { + *out = input.substr(0, divider); + out++; + + input = input.substr(divider+delimiter.length()); + } + } +} + +template +Container split(std::string input, std::string delimiter) +{ + Container result; + + split(input, delimiter, std::back_inserter(result)); + + return result; +} + +#endif /* end of include guard: UTIL_H_1F6A84F6 */ diff --git a/vendor/rawr-ebooks b/vendor/rawr-ebooks index ba25493..247ee4d 160000 --- a/vendor/rawr-ebooks +++ b/vendor/rawr-ebooks @@ -1 +1 @@ -Subproject commit ba25493b55b4e4e35de3fca69afd15ddcbaa545c +Subproject commit 247ee4de24eab5ecd030542724db9f69aaa1ed1a -- cgit 1.4.1