How to extract small video chunk with ffmpeg?

2019-08-23 11:02发布

I'm writing a script to split a video in multiple small files based on subtitles timing. Each chunk illustrated how to sign a word LSF (French Sign Language).

However, some file are blank once extracted and when converted to webm they are 0kb.

Timing data

I extract the timing from a .ass file to get this kind of file

0:00:01.01 0:00:04.07
0:00:04.09 0:00:07.00
0:00:07.00 0:00:10.36
0:00:10.36 0:00:13.28

First column is start_time, second is end_time

Extraction

extract_word_chunk() {
  ffmpeg \
    -i "$INPUT_FILE" \
    -ss "$start" \
    -to "$end" \
    -vcodec copy \
    -acodec copy \
    -loglevel error \
    "$chunk" < /dev/null
}

Conversion to webm

convert_to_webm() {
  ffmpeg \
    -y \
    -i "$chunk" \
    -acodec libvorbis \
    -codec:v libvpx \
    -b:v 192k \
    -b:a 96k \
    -minrate 128k \
    -maxrate 256k \
    -bufsize 192k \
    -quality good \
    -cpu-used 2 \
    -deadline best \
    -loglevel error \
  "$chunk.webm" < /dev/null
}

Output

# extract_word_chunk
ffmpeg \
  -i 'assets/raw/partie 1: Apprendre 300 mots du quotidien en LSF.jauvert laura.mkv' \
  -ss 0:00:01.01 \
  -to 0:00:04.07 \
  -vcodec copy \
  -acodec copy \
  -loglevel error \
  assets/raw/0:00:01.01.mkv

# convert_to_webm
ffmpeg -y \
  -i assets/raw/0:00:01.01.mkv \
  -acodec libvorbis \
  -codec:v libvpx \
  -b:v 192k \
  -b:a 96k \
  -minrate 128k \
  -maxrate 256k \
  -bufsize 192k \
  -quality good \
  -cpu-used 2 \
  -deadline best \
  -loglevel error \
  assets/raw/0:00:01.01.mkv.webm

Error

[buffer @ 0x16d8be0] Unable to parse option value "-1" as pixel format
    Last message repeated 1 times
[buffer @ 0x16d8be0] Error setting option pix_fmt to value -1.
[graph 0 input from stream 0:0 @ 0x16e6fc0] Error applying options to the filter.

Question

Only some chunk are blank/empty.

How do I prevent my video to be blank/empty?

1条回答
爷的心禁止访问
2楼-- · 2019-08-23 12:02

Do it in one command

ffmpeg -y \
  -i 'assets/raw/partie 1: Apprendre 300 mots du quotidien en LSF.jauvert laura.mkv' \
  -ss 0:00:01.01 \
  -to 0:00:04.07 \
  -acodec libvorbis \
  -codec:v libvpx \
  -b:v 192k \
  -b:a 96k \
  -minrate 128k \
  -maxrate 256k \
  -bufsize 192k \
  -quality good \
  -cpu-used 2 \
  -deadline best \
  -loglevel error \
  assets/raw/0:00:01.01.mkv.webm

When you use copy mode, and decode seek (-ss after -i) ffmpeg only cuts at keyframes, so sometimes there may not be any keyframes in the time range specified, hence those output mkvs are empty. You can avoid this by extracting and encoding in one step.

查看更多
登录 后发表回答