Giter Club home page Giter Club logo

video-writer's Introduction

video-writer

Super fast video writer for opencv using ffmpeg and libav (C++ and python)

The Problem

Opencv's cv::VideoWriter is a pain to use.

  • It hides the critical choice of encoders behind an obscure fourcc code and it isn't always transparent which codecs are available for use on the system. This is because opencv depends on a number of backends like ffmpeg and VFW. If you are someone who codes a lot on multiple systems (linux/mac/win/x86/arm) and embedded boards, this can quickly become frustrating.
  • Even setting aside the issue of codecs, cv::VideoWriter doesn't let you set important parameters like bitrate and pixel format.

Wouldn't it be nice if there were a lightweight and transparent API for writing videos using hardware acceleration on python and C++?

The easy solution

Unsurprisingly, many other people have noticed the issue and the most upvoted solution on stackoverflow recommends (at least for python) opening a subprocess with ffmpeg and passing frames as JPEGs. Now there are better formats to pass frames into ffmpeg than JPEG and you would be better off really adapting this pipeline for your own usecase, but the bigger problem is that due to the limitations of python multiprocessing, the shells created this way are not freed as long as the parent python program is running (effin' GIL). This lead of all kinds of ugliness. For example, if you are making many videos from one python script, videos which have finished writing will not be viewable in vlc/ffplay/etc until all the videos have finished processing and the parent python script has exited.

Why this project?

In spite of its limitations, the easy solution works and 99% of users don't need anything else, but if video processing is an essential part of your workflow, you might find it worthwhile to invest in a more satisfying solution. This is where this project comes in. We use LibAV, the backend behind ffmpeg to build a simple video writer object which can be used in C++ an python.

Status

This is a work in progress. Please do not use it in anything critical and feel free to contribute by sending PRs.

Build

Currently verified on macOS.

#Install dependencies
brew install ffmpeg pkg-config cmake

#Clone repo
git clone https://github.com/dataplayer12/video-writer.git
cd video-writer
mkdir build
cd build

#Configure and build project
cmake ../
make

make will generate a minimum C++ sample and a shared library which can be used in a python script with ctypes module.

How to use and To-Dos

The goal is to create a VideoWriter class which can be instantiated like

//C++
VideoWriter writer("filename.mp4", fps, width, height, encoder_name, bitrate);
writer.write(cvFrame); //cvFrame is a cv::Mat object
#python
import video_writer as vw
import numpy as np

writer= vw.VideoWriter(filename, fps, width, height, encoder_name, bitrate)
x=np.ones((height, width, 3), dtype=np.uint8)
writer.write(x)

Currently the write method accepts pointers and is not ready for use in python.

  • Make write method functional.
  • Add support for writing cvMat as frame.
  • Add support for python.
  • Make C++ and python examples

Credits

In making this project, I have learnt a lot from the excellent work of Bartholomew Joyce, his videos and git repo. It helped me set up the environment and get a feel for the functions of libav. The difference between his repo and this is that I want to encode rather than decode.

video-writer's People

Contributors

dataplayer12 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.