diff --git a/ffmpeg-mka-thumbnailer b/ffmpeg-mka-thumbnailer index 7bc8438..66f6f77 100755 --- a/ffmpeg-mka-thumbnailer +++ b/ffmpeg-mka-thumbnailer @@ -64,14 +64,23 @@ if [[ ! (-d "$dir" && -w "$dir") ]] { exit 1 } -attachment_streams="$(ffprobe -of json -v error -select_streams t -show_streams "$input" | jq -c)" +function ffmpeg_with_args() { + ffprobe -of json -v error -select_streams $1:m:filename -show_streams "$input" +} + +JQ_SELECTOR='[.streams, inputs.streams] as $inputs | {streams: ($inputs[0] - $inputs[1] + $inputs[2])}' +attachment_streams="$(jq -c "$JQ_SELECTOR" <<(ffmpeg_with_args v) <<(ffmpeg_with_args V) <<(ffmpeg_with_args t))" if [[ "$(echo -n "$attachment_streams" | jq ".streams | length")" -lt 1 ]] { - echo "Input does not have any attachment streams" >&2 + echo "Input does not have any suitable attachment streams" >&2 exit 1 } -attachment_filename="$(echo -n "$attachment_streams" | jq -r '[.streams[] | select((.tags.filename | (startswith("cover") or startswith("small_cover"))) and (.tags.mimetype | startswith("image/")))] | sort_by(.index) | first | .tags.filename | strings')" +JQ_SELECTOR='[.streams[] | select((.tags.filename | (startswith("cover") or startswith("small_cover"))) and (.tags.mimetype | startswith("image/")))] | sort_by(.index) | first | .tags.filename | strings' +attachment_filename="$(echo -n "$attachment_streams" | jq -r "$JQ_SELECTOR")" + +JQ_SELECTOR="[.streams[] | select(.tags.filename == \"$attachment_filename\")] | first | .codec_type" +attachment_codec_type="$(echo -n "$attachment_streams" | jq -r "$JQ_SELECTOR")" if [[ "$attachment_filename" = "" ]] { echo "No suitable attachment found" >&2 @@ -90,7 +99,11 @@ if [[ $tmpdir_success -ne 0 ]] { return_code=0 repeat 1 { - ffmpeg -v error -dump_attachment:t "$attachment_filename" -f matroska -i "$input" -t 0 -f null null || { return_code=$?; break } + if [[ "$attachment_codec_type" = "video" ]] { + ffmpeg -v error -f matroska -i "$input" -map m:filename:"$attachment_filename" -c copy "$attachment_filename" || { return_code=$?; break } + } else { + ffmpeg -v error -dump_attachment:t "$attachment_filename" -f matroska -i "$input" -t 0 -f null null || { return_code=$?; break } + } if [[ "${attachment_filename##*.}" = "${output##*.}" || ("${attachment_filename##*.}" =~ ^jpe?g$ && "${output##*.}" =~ ^jpe?g$) ]] { mv "$attachment_filename" "$output" } else {