简体   繁体   中英

Video Thumbnail creation in landscape or portrait mode

I'm using Nreco Video converter to create video thumbnails. Here is the C# code that I'm using.

(new NReco.VideoConverter.FFMpegConverter()).GetVideoThumbnail(fileSource, thumbNailPath, (float)0.1);

It simply works fine. The only issue being the orientation. The videos for which I'm trying to create thumbnails are recorded on a mobile app. So irrespective of whether the video is in portrait or landscape mode, the thumbnail generated is randomly in portrait or landscape mode.

Does any one know how to create a thumbnail of a video in a particular mode(landscape or portrait).

There is a rotation-parameter in video files that you can read by using various other ffmpeg wrapper libraries. Many players use it to actually rotate the screen. See here . As NReco does not support this directly, you would have to read this value with some other library and use it to rotate the jpeg in the stream.

I suggest using a ffmpeg wrapper where you can directly invoke ffmpeg process instances, as ffmpeg is able to read various properties from the file.

You can use ffmpeg for getting rotation from video metadata and apply appropriate rotation filter during thumbnail extraction. Since NReco VideoConverter is a .NET ffmpeg wrapper it also can be used for doing that:

  1. Extract video orientation metadata from ffmpeg console (LogReceived event) with Invoke or ConvertMedia methods that actually doesn't perform any conversion. Rotation data can be matched with simple regex.
  2. Compose FFMpeg arguments for appropriate rotatation filter (like: -vf "transpose=1" )
  3. Extract thumbnail with ConvertMedia method that accepts extra ffmpeg command line arguments (see code snippet below)

(internally GetVideoThumbnail uses ConvertMedia method):

var thumbSettings = new ConvertSettings() {
    VideoFrameCount = 1,
    VideoFrameRate = 1,
    MaxDuration = 1, // extract exactly 1 frame
    Seek = 0, // frame seek position
    CustomOutputArgs = String.Format(" -vf \"{0}\"", rotateFilter )   // rotation filter parameters
};
ffMpegConverter.ConvertMedia(inputFile1, null, thumbJpegOutputStream, "mjpeg", thumbSettings);

As result you will get video thumnail rotated according to video orientation metadata. Full code that implements all steps can be found in VideoConverter package (Rotate example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM