sy_ar001
sy_ar001

Reputation: 369

How to trim webm video while preserving transparency

I want to trim a transparent webm video using ffmpeg. Here's the ffprobe result for that video:

Input #0, matroska,webm, from 'template.webm':
  Metadata:
    ENCODER         : Lavf58.29.100
  Duration: 00:00:05.24, start: -0.002000, bitrate: 2856 kb/s
    Stream #0:0: Video: vp8, yuv420p(progressive), 1573x900, SAR 1:1 DAR 1573:900, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      ALPHA_MODE      : 1
      ENCODER         : Lavc58.54.100 libvpx
      DURATION        : 00:00:05.240000000
    Stream #0:1: Audio: opus, 48000 Hz, mono, fltp
    Metadata:
      ENCODER         : Lavc58.54.100 libopus
      DURATION        : 00:00:05.241000000

I tried

ffmpeg -i template.webm -ss 1 -to 3 -c copy trimmed.webm

but the trimmed video doesn't start (or sometimes end) at the exact times defined in the command so I tried re-encoding the video using libvpx

ffmpeg -i template.webm -ss 1 -to 3 -c:v libvpx -c:a copy -crf 30 -b:v 0 trimmed.webm

It solved the timing issue but this results in loss of transparency of output video. Here's the ffprobe:

Input #0, matroska,webm, from 'trimmed.webm':
  Metadata:
    ENCODER         : Lavf57.83.100
  Duration: 00:00:02.00, start: -0.001000, bitrate: 1395 kb/s
    Stream #0:0: Video: vp8, yuv420p(progressive), 1573x900, SAR 1:1 DAR 1573:900, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      ALPHA_MODE      : 1
      ENCODER         : Lavc57.107.100 libvpx
      DURATION        : 00:00:02.000000000
    Stream #0:1: Audio: opus, 48000 Hz, mono, fltp
    Metadata:
      ENCODER         : Lavc58.54.100 libopus
      DURATION        : 00:00:02.001000000

How should I trim the video while preserving the transparency? Moreover, a fast solution will be extremely helpful.

Upvotes: 0

Views: 1885

Answers (1)

llogan
llogan

Reputation: 134293

The native, built-in FFmpeg VP8 decoder does not yet support alpha/transparency. Use libvpx to decode:

ffmpeg -c:v libvpx -i template.webm -ss 1 -to 3 -c:v libvpx -c:a copy -crf 30 -b:v 0 trimmed.webm

If you get Transparency encoding with auto_alt_ref does not work error then add the -auto-alt-ref 0 output option or change -c:v libvpx output option to -c:v libvpx-vp9.

Upvotes: 1

Related Questions