Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wandenberg/nginx-video-thumbextractor-module

Nginx module to extract thumbs from a video file
https://github.com/wandenberg/nginx-video-thumbextractor-module

Last synced: about 2 months ago
JSON representation

Nginx module to extract thumbs from a video file

Awesome Lists containing this project

README

        

h1(#nginx_video_thumbextractor_module). Nginx Video Thumb Extractor Module

Video Thumb Extractor is a module to extract an image from a video frame from a specific second resizing/cropping it to a given size.
The smallest generated image is ==16x16== pixels.

_This module is not distributed with the Nginx source. See "the installation instructions":installation._

Available on github at "nginx-video-thumbextractor-module":repository

h1(#status). Status

This module is considered production ready.

h1(#requirements). Requirements

This module depends from some libraries (headers and shared objects) which has to be installed before it:

* avformat >= 57.56.101 (last tested version: 58.12.100) - commonly distributed with "FFmpeg":http://ffmpeg.org
* avcodec >= 57.64.101 (last tested versions: 58.18.100) - commonly distributed with "FFmpeg":http://ffmpeg.org
* avutil >= 55.34.101 (last tested versions: 56.14.100) - commonly distributed with "FFmpeg":http://ffmpeg.org
* avfilter >= 6.65.100 (last tested versions: 7.16.100) - commonly distributed with "FFmpeg":http://ffmpeg.org
* swscale >= 4.2.100 (last tested versions: 5.1.100) - commonly distributed with "FFmpeg":http://ffmpeg.org
* jpeg - "libjpeg":http://libjpeg.sourceforge.net

h1(#recommendation). Recommendation

When installing FFmpeg (last tested versions: 4.0.6) from source do not forget to enable shared libraries using

pre. --enable-shared

If you don't install FFmpeg from source you need to be sure that you have headers files for the libs.

h1(#supported_video_formats). Supported Video Formats

This module uses the libraries avcodec and avformat to read the video files. Any supported video format for these libraries will work.
Tested formats was _mp4_, _mov_ and _flv_.

h1(#installation). Installation

Install the above requirements and follow the steps bellow.


# clone the project
git clone https://github.com/wandenberg/nginx-video-thumbextractor-module.git
NGINX_VIDEO_THUMBEXTRACTOR_MODULE_PATH=$PWD/nginx-video-thumbextractor-module

# get desired nginx version (tested with 1.18.x series)
wget http://nginx.org/download/nginx-1.18.0.tar.gz

# unpack, configure and build
tar xzvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
# configure nginx
./configure --add-module=$NGINX_VIDEO_THUMBEXTRACTOR_MODULE_PATH
make

# install and finish
sudo make install

# check
sudo /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0

# test configuration
sudo /usr/local/nginx/sbin/nginx -c $NGINX_VIDEO_THUMBEXTRACTOR_MODULE_PATH/nginx.conf -t
the configuration file $NGINX_VIDEO_THUMBEXTRACTOR_MODULE_PATH/nginx.conf syntax is ok
configuration file $NGINX_VIDEO_THUMBEXTRACTOR_MODULE_PATH/nginx.conf test is successful

# run
sudo /usr/local/nginx/sbin/nginx -c $NGINX_VIDEO_THUMBEXTRACTOR_MODULE_PATH/nginx.conf

h1(#basic-configuration). Basic Configuration


location ~ /thumbs(.*) {
video_thumbextractor;
video_thumbextractor_video_filename $1;
video_thumbextractor_video_second $arg_second;
video_thumbextractor_image_width $arg_width;
video_thumbextractor_image_height $arg_height;
}

h1(#basic-usage). Basic Usage

Assuming that you have a file called _video.mp4_ on your root folder use a browser to test the following urls:


# get an image from second 10 with the original size
http://localhost/thumbs/video.mp4?second=10

# get an image from second 20 with a 50px of height and proportional width keeping video scale
http://localhost/thumbs/video.mp4?second=10&height=50

# get an image from second 30 with a 50px of height and 100px of width, the image will be cropped to keep video scale
http://localhost/thumbs/video.mp4?second=20&height=50&width=100

h1(#directives). Directives

h2(#video_thumbextractor). video_thumbextractor

*syntax:* _video_thumbextractor_
*context:* _location_
*release version:* _0.1.0_

Set Video Thumb Extractor as the request handler for the location.

h2(#video_thumbextractor_video_filename). video_thumbextractor_video_filename

*syntax:* _video_thumbextractor_video_filename filename_
*default:* _none_
*context:* _http_
*release version:* _0.1.0_

The video filename relative to root folder.
This directive is required.
Return a 404 if the video is not found.

h2(#video_thumbextractor_video_second). video_thumbextractor_video_second

*syntax:* _video_thumbextractor_video_second second_
*default:* _none_
*context:* _http_
*release version:* _0.1.0_

The time in seconds where the image should be extracted. The nearest key frame will be used to get the image.
This directive is required.
Return a 400 if the value is not specified.
Return a 404 if the second is not found (the video is shorter than the time specified).

h2(#video_thumbextractor_image_width). video_thumbextractor_image_width

*syntax:* _video_thumbextractor_image_width width_
*default:* _0_
*context:* _http_
*release version:* _0.1.0_

The width used to generate the image.
This directive is optional.
If only the width is specified the video size will be used as image size.

h2(#video_thumbextractor_image_height). video_thumbextractor_image_height

*syntax:* _video_thumbextractor_image_height height_
*default:* _0_
*context:* _http_
*release version:* _0.1.0_

The height used to generate the image.
This directive is optional.
If only the height is specified the width will be determined using video scale to keep the aspect.
If both, width and height, are specified the image will suffers a resize and an eventual crop to keep the aspect.

h2(#video_thumbextractor_only_keyframe). video_thumbextractor_only_keyframe

*syntax:* _video_thumbextractor_only_keyframe on|off_
*default:* _on_
*context:* _http_
*release version:* _0.3.0_

Set if only the keyframes should be used to create the image.
When set to off, the process will be a bit slower because the video will be decoded from the nearest keyframe up to the exact requested time.

h2(#video_thumbextractor_next_time). video_thumbextractor_next_time

*syntax:* _video_thumbextractor_next_time on|off_
*default:* _on_
*context:* _http_
*release version:* _0.3.0_

Set if will use the previous or the next keyframe considering the requested time when configured to use only the keyframes.

h2(#video_thumbextractor_tile_rows). video_thumbextractor_tile_rows

*syntax:* _video_thumbextractor_tile_rows number_
*default:* _none_
*context:* _http_
*release version:* _0.6.0_

Set the number of rows to be used on tile filter. The number of cols will be calculated using the sample interval, the video duration and the start time used on 'video_second' directive, if not set.

!test/test_video_1_rows.jpg(using only 1 row)!

h2(#video_thumbextractor_tile_cols). video_thumbextractor_tile_cols

*syntax:* _video_thumbextractor_tile_cols number_
*default:* _none_
*context:* _http_
*release version:* _0.6.0_

Set the number of cols to be used on tile filter. The number of rows will be calculated using the sample interval, the video duration and the start time used on 'video_second' directive, if not set.

!test/test_video_2_cols.jpg(using 2 cols)!

h2(#video_thumbextractor_tile_max_rows). video_thumbextractor_tile_max_rows

*syntax:* _video_thumbextractor_tile_max_rows number_
*default:* _none_
*context:* _http_
*release version:* _0.6.0_

Set the max number of rows to be used on tile filter when only the number of cols was set. After calculate the number of necessary rows it can be limited by the configured max number of rows.

!test/test_video_2_cols_2s_interval_2_max_rows.jpg(using 2 cols limited on up to 2 rows)!

h2(#video_thumbextractor_tile_max_cols). video_thumbextractor_tile_max_cols

*syntax:* _video_thumbextractor_tile_max_cols number_
*default:* _none_
*context:* _http_
*release version:* _0.6.0_

Set the max number of cols to be used on tile filter when only the number of rows was set. After calculate the number of necessary cols it can be limited by the configured max number of cols.

!test/test_video_1_rows_2s_interval_5_max_cols.jpg(using only 1 row limited on up to 5 cols)!

h2(#video_thumbextractor_tile_sample_interval). video_thumbextractor_tile_sample_interval

*syntax:* _video_thumbextractor_tile_sample_interval number_
*default:* _none_
*context:* _http_
*release version:* _0.6.0_

Set the sample interval to get the frames for tile. When it was not set and the number of cols and rows were, the sample interval will be calculated to the video can fit on the tile layout.
When only the number of rows or the number of cols was set and sample interval was not, the module will use the default value of 5 seconds.
To the interval be respected you should set video_thumbextractor_only_keyframe directive to off.

!test/test_video_2_cols_2s_interval.jpg(using 2 cols and 2 seconds of interval)!

!test/test_video_1_rows_2s_interval.jpg(using 1 row and 2 seconds of interval)!

!test/test_video_4_cols_4_rows.jpg(using 4 rows, 4 cols and calculated sample interval)!

!test/test_video_4_cols_4_rows_3s_interval.jpg(using 4 rows, 4 cols and 3 seconds of interval)!

h2(#video_thumbextractor_tile_color). video_thumbextractor_tile_color

*syntax:* _video_thumbextractor_tile_color string_
*default:* _none_
*context:* _http_
*release version:* _0.6.0_

Set the color to be used on the background. Valid values are defined "here":https://ffmpeg.org/ffmpeg-utils.html#Color

!test/test_video_2_cols_bg_color.jpg(using #EEAA33 as background color)!

h2(#video_thumbextractor_tile_margin). video_thumbextractor_tile_margin

*syntax:* _video_thumbextractor_tile_margin number_
*default:* _none_
*context:* _http_
*release version:* _0.6.0_

Set the size of the margin to be used on the final image.

!test/test_video_2_cols_2_rows_5_margin.jpg(using 5px of margin)!

h2(#video_thumbextractor_tile_padding). video_thumbextractor_tile_padding

*syntax:* _video_thumbextractor_tile_padding number_
*default:* _none_
*context:* _http_
*release version:* _0.6.0_

Set the size of the padding to be used between the frames.

!test/test_video_2_cols_2_rows_3_padding.jpg(using 3px of padding)!

h2(#video_thumbextractor_threads). video_thumbextractor_threads

*syntax:* _video_thumbextractor_threads string_
*default:* _auto_
*context:* _http_
*release version:* _0.6.0_

Set the number of threads used by avcodec.

h2(#video_thumbextractor_processes_per_worker). video_thumbextractor_processes_per_worker

*syntax:* _video_thumbextractor_processes_per_worker number_
*default:* _1_
*context:* _http_
*release version:* _0.7.0_

Set the number of process each nginx worker can fork to extract the thumbs. The requests will be queued until there is an available process.

h1(#contributors). Contributors

"People":contributors

h1(#changelog). Changelog

h2(#0_9_0). v0.9.0
* drop support to versions prior Nginx 1.10.0 and FFmpeg libraries prior to 3.2.4

h2(#0_8_0). v0.8.0
* add support to use dynamic values on tile configurations

h2(#0_7_0). v0.7.0

* add support to execute the thumb extraction in a different process to not block the nginx worker
* add video_thumbextractor_processes_per_worker directive
* try to make the code simpler
* revert the changes on 0.6.2

h2(#0_6_2). v0.6.2

* trying to improve the performance not reading the raw response (the video)

h2(#0_6_1). v0.6.1

* add support to rotate the image when the video stream has instructions to do it
* add support to videos with different aspect ratio
* fix support to files with more than one video stream choosing the best of them

h2(#0_6_0). v0.6.0

* add support to tile filter, used to make story boards or sprites
* add directive to control the number of threads used by avcodec

h2(#0_5_0). v0.5.0

* fix jpeg dpi configuration usage
* remove support to old libav* libraries
* remove MagickWand dependency which cause memory leak

h2(#0_4_0). v0.4.0

* fix to proper read videos with the moov atom is at the end of the file
* fix to use the nearest decoded frame, in the case of requested time is on the end of the video
* fix CFLAGS, LDFLAGS and ngx_feature_libs to find the parameters to compile using right MagickWand lib version
* using av_frame_alloc instead of avcodec_alloc_frame when available

h2(#0_3_0). v0.3.0

* fix frame allocation assert
* add configuration to choose between only keyframes or not, and if will use the previous or next frame relative to given time
* make possible read the video from an nginx cache file

h2(#0_2_0). v0.2.0

* fix use of deprecated functions

h2(#0_1_0). v0.1.0

* Initial release

[repository]https://github.com/wandenberg/nginx-video-thumbextractor-module
[installation]#instalation
[contributors]https://github.com/wandenberg/nginx-video-thumbextractor-module/contributors