nrk.no

Using F# and FFmpeg to take thumbnails from video

Kategori: Dev

Screenshot

On a current side project I needed to create thumbnails on given timestamps in NRK programmes. After browsing around a bit, I found a quote that sums it up: “All of them (the links) say «Use FFMPEG OpenSource Project» for the job. “
Here are the steps taken to achieve a working solution and some performance tips learned underway.

  1. Getting the FFmpeg executable and placing it on local drive. A quick search and you will find it, there are a few builds for windows, choose the most appropriate for your environment.
  2. A quick test of a command (ffmpeg -y -i \\someSan\6\DVNR04\00\DVNR04003814\DVNR04003814AA_ID720.mp4 -s 50×50 -ss 00:02:10.100 -f image2 -vframes 1 out.png) worked beautifully, below is a screenshot of the output:
    Høne output
  3. I needed to write a programme to automate this. The programme needed logic to find the files on a SAN. Calculate the proper filenames based on programme Id and time. And lastly to execute the FFmpeg snap process. Below is a gist of the programme.
  4. When first trying to run the code on 10 programmes it was really slow. In NRK all our programmes come in 5 qualities and I was using the highest one (720p) to take the thumbnails. Using the highest quality for a 96×54 snap is a bit overkill so i switched to the lowest which was the first speed improvement.
  5. Below is a benchmark run on 10 programmes, it took 58 snaps in 6 minutes, not so great.benchmark_6min
  6. A greater improvement was to put the -ss option in front of the -i option since that makes FFmpeg seeking use input as keyframes not decoding frame-by-frame. This really sped things up and the new benchmark showed improvement from about 6 minutes to 1 minute 18 seconds.benchmark_1min

Things that could have been done differently/improved

Most of the FFmpeg options are hard coded, to fit my needs, should the need arise they should of course be made configurable.

 

I am pretty happy with the solution, it has worked flawlessly so far. Please feel free to comment or/and code review in english or norwegian.

4 kommentarer

    • Erlend Wiig (NRK) (svar til HC)

      Tenker du på innhenting av informasjon, dvs bygge opp command delen? Redd at bottlenecken i paralell fort blir CPU bruk av FFmpeg prosessen.

Legg igjen en kommentar

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *. Les vår personvernserklæring for informasjon om hvilke data vi lagrer om deg som kommenterer.