Opened 16 months ago

Last modified 2 months ago

#10200 new defect

Piping WEBP image will produce a broken image

Reported by: lolcat Owned by:
Priority: normal Component: ffmpeg
Version: 4.3.5 Keywords: webp, sigpipe
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description (last modified by lolcat)

Hello. I've been trying to convert a video to an animated webp thumbnail image. This command doesn't work, because the output is piped. When I output to a file on disk, this problem does not occur. Here is my command:

ffmpeg -i 'https://th.bing.com/th?id=OMB1.7pHOr_m7KOKS3w&pid=2.1' -vf "fps=10,scale=148:83:flags=lanczos" -vcodec libwebp -lossless 0 -compression_level 6 -q:v 50 -loop 0 -preset picture -an -f webp - > test.webp

But if I use the following command:

ffmpeg -i 'https://th.bing.com/th?id=OMB1.7pHOr_m7KOKS3w&pid=2.1' -vf "fps=10,scale=148:83:flags=lanczos" -vcodec libwebp -lossless 0 -compression_level 6 -q:v 50 -loop 0 -preset picture -an -f webp out.webp

The image will load just fine in all modern browsers (notice at the end, I'm outputting to a file instead of a pipe). When using hexedit, I found out that the bytes 5 to 7 are all 0s, when it should be 88 B4 05. Patching these bytes in manually magically fixes the image.

UPDATE:
I investigated the bug a little, and it seems that replacing the bytes I talked about only fixes the issue *sometimes*. When trying to convert this URL: https://th.bing.com/th?id=OMB1.7pHOr_m7KOKS3w it does not yield any errors in both ffmpeg and any image decoder that supports it. But if I try to convert the video I linked in the original question (first code snippet), I get a corruption in the middle of the file. I know this because I am piping the output of ffmpeg into the Firefox browser, and in the console, even though I can see that the image started animating, it cuts off and tells me that the file is either "truncated, or corrupt". I established that it was not truncated, and truly corrupted, since the filesize is not different from something outputted by the second command ...

I'm at a lost here. This sounds like a pipe encoder issue, and I don't have the expertise to fix this. If this helps anyone, I will post my (PHP) attempt at fixing the file(s)

<?php

$video = escapeshellarg($_GET["v"]);

header("Content-Type: image/webp");
//header("Content-Type: text/plain");
$handle = popen(
        "ffmpeg -i {$video} -vf \"fps=10,scale=148:83:flags=lanczos\" -vcodec libwebp -lossless 0 -compression_level 6 -q:v 50 -loop 0 -preset picture -an -f webp -",
        "rb"
);

if($handle === false){
        
        // ffmpeg call failed
        return;
}

$bytemod = false;

while(!feof($handle)){
        
        if($bytemod === false){
                
                echo substr_replace(fread($handle, 7), hex2bin("88b405"), 4, 3);
                $bytemod = true;
                continue;
        }
        
        echo fread($handle, 1);
}

fclose($handle);

Change History (3)

comment:1 by lolcat, 16 months ago

Description: modified (diff)

comment:2 by lolcat, 16 months ago

Update #2:
I compiled FFMPEG to see if the issue had been fixed. It has not. It still generates broken images when piped.

comment:3 by mkver, 2 months ago

Hint: If you want an animated WebP image, you need to use the libwebp_anim encoder and not libwebp.

Note: See TracTickets for help on using tickets.