[FFmpeg-user] Input/output error for MJPEG (with examples)

Renato Santos renato.cron at gmail.com
Wed Mar 26 08:06:39 CET 2014


Hello again,

I found this MJPEG stream
http://217.197.157.7:7070/mjpg/video.mjpg

in a list on internet, and i'm testing ffmpeg and my software.

When I pass it do ffmpeg, it reports Input/output error and stop,

/home/renato/ffmpeg/ffmpeg-HEAD-e2742d6/ffmpeg -loglevel debug -an  -f
mjpeg -timeout 10 -user-agent 'Mozilla/4.0 (compatible; MSIE 8.0; Windows
NT 5.1; Trident/4.0)' -i 'http://217.197.157.7:7070/mjpg/video.mjpg' -vf
scale="'if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'" -vframes 1
 /tmp/snap_22342527856247655680.jpg
ffmpeg version 2.1.git-e2742d6 Copyright (c) 2000-2013 the FFmpeg developers
  built on Dec 16 2013 13:41:29 with gcc 4.7 (Ubuntu/Linaro 4.7.2-2ubuntu1)
  configuration: --enable-gpl --enable-libx264 --prefix='~/ffmpeg/'
  libavutil      52. 58.100 / 52. 58.100
  libavcodec     55. 45.101 / 55. 45.101
  libavformat    55. 22.100 / 55. 22.100
  libavdevice    55.  5.102 / 55.  5.102
  libavfilter     3. 92.100 /  3. 92.100
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.104 /  0. 17.104
  libpostproc    52.  3.100 / 52.  3.100
Splitting the commandline.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging
level) with argument 'debug'.
Reading option '-an' ... matched as option 'an' (disable audio) with
argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument
'mjpeg'.
Reading option '-timeout' ... matched as AVOption 'timeout' with argument
'10'.
Reading option '-user-agent' ... matched as AVOption 'user-agent' with
argument 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)'.
Reading option '-i' ... matched as input file with argument '
http://217.197.157.7:7070/mjpg/video.mjpg'.
Reading option '-vf' ... matched as option 'vf' (set video filters) with
argument 'scale='if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)''.
Reading option '-vframes' ... matched as option 'vframes' (set the number
of video frames to record) with argument '1'.
Reading option '/tmp/snap_22342527856247655680.jpg' ... matched as output
file.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option loglevel (set logging level) with argument debug.
Successfully parsed a group of options.
Parsing a group of options: input file
http://217.197.157.7:7070/mjpg/video.mjpg.
Applying option an (disable audio) with argument 1.
Applying option f (force format) with argument mjpeg.
Successfully parsed a group of options.
Opening an input file: http://217.197.157.7:7070/mjpg/video.mjpg.
[http @ 0x98d84a0] request: GET /mjpg/video.mjpg HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
Accept: */*
Range: bytes=0-
Connection: close
Host: 217.197.157.7:7070


http://217.197.157.7:7070/mjpg/video.mjpg: Input/output error


But.. I can play it on Chrome and I can save each frame using this script
too:

http://pastebin.com/CcjLfngC

use strict;
use LWP::UserAgent;

LWP::UserAgent->new->get("http://217.197.157.7:7070/mjpg/video.mjpg",
':content_cb' => \&got_data); # happiness

my $buffer = '';
my $boundary;
my $x = 0;
sub got_data {
    my ($str, $ref) = @_;


    $boundary = do {
        my $z = $ref->headers->{'content-type'} =~ /boundary=(.+)\;?/;
        $1} if !defined $boundary;

    $buffer .= $str;
    next unless $boundary;

    #print $buffer;

    while( 1+(my $end = index($buffer, $boundary))) {

        my $ret = substr $buffer, 0, $end, '';

        handle_part($ret) if $ret ne '--';

        substr $buffer, 0, length($boundary), '';
    }

}

my $idx = 0;

sub handle_part {
    my $data = shift;
    return unless length $data;

    # clear some headers
    $data =~ s/^.+Content-Length\: \d+//s;
    $data =~ s/^.+Content-type\: image\/jpeg//s;

    $data =~ s/^(\r|\n)+//;


    $idx++;
    print "$idx ".time."\n";
    open my $fh, '>:raw', "/tmp/images-$idx.jpg";
    print $fh $data;
    #print $data;
    close $fh;
}


Images on /tmp/images-XXX.jpg.

The headers are fine, I guess:

Cache-Control: no-cache
Connection: close
Pragma: no-cache
Content-Type: multipart/x-mixed-replace; boundary=myboundary
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Client-Peer: 217.197.157.7:7070
Client-Response-Num: 1

 then each segment with

--myboundary
Content-Type: image/jpeg
Content-Length: *44363*

*<binary>*

So, like my last e-mail i added '-timeout 10', i removed it and tried again:

renato at ood:/projetos/aware/etamu/lost-n-found$ time
/home/renato/ffmpeg/ffmpeg-HEAD-e2742d6/ffmpeg -loglevel debug -an  -f
mjpeg  -user-agent 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1;
Trident/4.0)' -i 'http://217.197.157.7:7070/mjpg/video.mjpg' -vf
scale="'if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'" -vframes 1
 /tmp/snap_22342527856247655680.jpg
ffmpeg version 2.1.git-e2742d6 Copyright (c) 2000-2013 the FFmpeg developers
  built on Dec 16 2013 13:41:29 with gcc 4.7 (Ubuntu/Linaro 4.7.2-2ubuntu1)
  configuration: --enable-gpl --enable-libx264 --prefix='~/ffmpeg/'
  libavutil      52. 58.100 / 52. 58.100
  libavcodec     55. 45.101 / 55. 45.101
  libavformat    55. 22.100 / 55. 22.100
  libavdevice    55.  5.102 / 55.  5.102
  libavfilter     3. 92.100 /  3. 92.100
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.104 /  0. 17.104
  libpostproc    52.  3.100 / 52.  3.100
Splitting the commandline.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging
level) with argument 'debug'.
Reading option '-an' ... matched as option 'an' (disable audio) with
argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument
'mjpeg'.
Reading option '-user-agent' ... matched as AVOption 'user-agent' with
argument 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)'.
Reading option '-i' ... matched as input file with argument '
http://217.197.157.7:7070/mjpg/video.mjpg'.
Reading option '-vf' ... matched as option 'vf' (set video filters) with
argument 'scale='if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)''.
Reading option '-vframes' ... matched as option 'vframes' (set the number
of video frames to record) with argument '1'.
Reading option '/tmp/snap_22342527856247655680.jpg' ... matched as output
file.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option loglevel (set logging level) with argument debug.
Successfully parsed a group of options.
Parsing a group of options: input file
http://217.197.157.7:7070/mjpg/video.mjpg.
Applying option an (disable audio) with argument 1.
Applying option f (force format) with argument mjpeg.
Successfully parsed a group of options.
Opening an input file: http://217.197.157.7:7070/mjpg/video.mjpg.
[http @ 0x97894c0] request: GET /mjpg/video.mjpg HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
Accept: */*
Range: bytes=0-
Connection: close
Host: 217.197.157.7:7070


[http @ 0x97894c0] header='HTTP/1.0 200 OK'
[http @ 0x97894c0] http_code=200
[http @ 0x97894c0] header='Cache-Control: no-cache'
[http @ 0x97894c0] header='Pragma: no-cache'
[http @ 0x97894c0] header='Expires: Thu, 01 Dec 1994 16:00:00 GMT'
[http @ 0x97894c0] header='Connection: close'
[http @ 0x97894c0] header='Content-Type: multipart/x-mixed-replace;
boundary=myboundary'
[http @ 0x97894c0] header=''
[mjpeg @ 0x9788e80] Before avformat_find_stream_info() pos: 0 bytes
read:2705 seeks:0
[mjpeg @ 0x9793100] marker=d8 avail_size_in_buf=44461
[mjpeg @ 0x9793100] marker parser used 0 bytes (0 bits)
[mjpeg @ 0x9793100] marker=e0 avail_size_in_buf=44459
[mjpeg @ 0x9793100] marker parser used 16 bytes (128 bits)
[mjpeg @ 0x9793100] marker=fe avail_size_in_buf=44441
[mjpeg @ 0x9793100] marker parser used 15 bytes (120 bits)
[mjpeg @ 0x9793100] marker=fe avail_size_in_buf=44424
[mjpeg @ 0x9793100] marker parser used 15 bytes (120 bits)
[mjpeg @ 0x9793100] marker=db avail_size_in_buf=44407
[mjpeg @ 0x9793100] index=0
[mjpeg @ 0x9793100] qscale[0]: 4
[mjpeg @ 0x9793100] marker parser used 67 bytes (536 bits)
[mjpeg @ 0x9793100] marker=db avail_size_in_buf=44338
[mjpeg @ 0x9793100] index=1
[mjpeg @ 0x9793100] qscale[1]: 9
[mjpeg @ 0x9793100] marker parser used 67 bytes (536 bits)
[mjpeg @ 0x9793100] marker=c0 avail_size_in_buf=44269
[mjpeg @ 0x9793100] sof0: picture: 800x600
[mjpeg @ 0x9793100] component 0 2:2 id: 0 quant:0
[mjpeg @ 0x9793100] component 1 1:1 id: 1 quant:1
[mjpeg @ 0x9793100] component 2 1:1 id: 2 quant:1
[mjpeg @ 0x9793100] pix fmt id 22111100
[mjpeg @ 0x9793100] marker parser used 17 bytes (136 bits)
[mjpeg @ 0x9793100] marker=c4 avail_size_in_buf=44250
[mjpeg @ 0x9793100] class=0 index=0 nb_codes=12
[mjpeg @ 0x9793100] marker parser used 31 bytes (248 bits)
[mjpeg @ 0x9793100] marker=c4 avail_size_in_buf=44217
[mjpeg @ 0x9793100] class=1 index=0 nb_codes=251
[mjpeg @ 0x9793100] marker parser used 181 bytes (1448 bits)
[mjpeg @ 0x9793100] marker=c4 avail_size_in_buf=44034
[mjpeg @ 0x9793100] class=0 index=1 nb_codes=12
[mjpeg @ 0x9793100] marker parser used 31 bytes (248 bits)
[mjpeg @ 0x9793100] marker=c4 avail_size_in_buf=44001
[mjpeg @ 0x9793100] class=1 index=1 nb_codes=251
[mjpeg @ 0x9793100] marker parser used 181 bytes (1448 bits)
[mjpeg @ 0x9793100] escaping removed 286 bytes
[mjpeg @ 0x9793100] marker=da avail_size_in_buf=43818
[mjpeg @ 0x9793100] component: 0
[mjpeg @ 0x9793100] component: 1
[mjpeg @ 0x9793100] component: 2
[mjpeg @ 0x9793100] marker parser used 43531 bytes (348244 bits)
[mjpeg @ 0x9793100] marker=d9 avail_size_in_buf=67
[mjpeg @ 0x9793100] decode frame unused 67 bytes
[mjpeg @ 0x9788e80] Probe buffer size limit of 5000000 bytes reached
[mjpeg @ 0x9788e80] After avformat_find_stream_info() pos: 5010305 bytes
read:5013601 seeks:0 frames:111
Input #0, mjpeg, from 'http://217.197.157.7:7070/mjpg/video.mjpg':
  Duration: N/A, bitrate: N/A
    Stream #0:0, 111, 1/1200000: Video: mjpeg, yuvj420p(pc), 800x600 [SAR
1:1 DAR 4:3], 1/25, 25 fps, 25 tbr, 1200k tbn, 25 tbc
Successfully opened the file.
Parsing a group of options: output file /tmp/snap_22342527856247655680.jpg.
Applying option vf (set video filters) with argument
scale='if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'.
Applying option vframes (set the number of video frames to record) with
argument 1.
Successfully parsed a group of options.
Opening an output file: /tmp/snap_22342527856247655680.jpg.
Successfully opened the file.
detected 8 logical cores
[Parsed_scale_0 @ 0x9797ee0] Setting 'w' to value 'if(gt(a,4/3),320,-1)'
[Parsed_scale_0 @ 0x9797ee0] Setting 'h' to value 'if(gt(a,4/3),-1,240)'
[Parsed_scale_0 @ 0x9797ee0] Setting 'flags' to value '0x4'
[Parsed_scale_0 @ 0x9797ee0] w:if(gt(a,4/3),320,-1) h:if(gt(a,4/3),-1,240)
flags:'0x4' interl:0
[graph 0 input from stream 0:0 @ 0x9798180] Setting 'video_size' to value
'800x600'
[graph 0 input from stream 0:0 @ 0x9798180] Setting 'pix_fmt' to value '12'
[graph 0 input from stream 0:0 @ 0x9798180] Setting 'time_base' to value
'1/1200000'
[graph 0 input from stream 0:0 @ 0x9798180] Setting 'pixel_aspect' to value
'1/1'
[graph 0 input from stream 0:0 @ 0x9798180] Setting 'sws_param' to value
'flags=2'
[graph 0 input from stream 0:0 @ 0x9798180] Setting 'frame_rate' to value
'25/1'
[graph 0 input from stream 0:0 @ 0x9798180] w:800 h:600 pixfmt:yuvj420p
tb:1/1200000 fr:25/1 sar:1/1 sws_param:flags=2
[format @ 0x9799800] compat: called with args=[yuvj420p|yuvj422p|yuvj444p]
[format @ 0x9799800] Setting 'pix_fmts' to value
'yuvj420p|yuvj422p|yuvj444p'
[AVFilterGraph @ 0x9c2bd20] query_formats: 4 queried, 3 merged, 0 already
done, 0 delayed
[swscaler @ 0x9777080] deprecated pixel format used, make sure you did set
range correctly
[Parsed_scale_0 @ 0x9797ee0] w:800 h:600 fmt:yuvj420p sar:1/1 -> w:320
h:240 fmt:yuvj420p sar:1/1 flags:0x4
[mjpeg @ 0x97cd5c0] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x9788aa0] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x9cb3640] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x9cd21c0] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x9cf0d40] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x9d0f8c0] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x9d2e440] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x9d4cfc0] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x9797140] intra_quant_bias = 96 inter_quant_bias = 0
Output #0, image2, to '/tmp/snap_22342527856247655680.jpg':
  Metadata:
    encoder         : Lavf55.22.100
    Stream #0:0, 0, 1/90000: Video: mjpeg, yuvj420p, 320x240 [SAR 1:1 DAR
4:3], 1/25, q=2-31, 200 kb/s, 90k tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg -> mjpeg)
Press [q] to stop, [?] for help
[mjpeg @ 0x9793100] marker=d8 avail_size_in_buf=44461
[mjpeg @ 0x9793100] marker parser used 0 bytes (0 bits)
[mjpeg @ 0x9793100] marker=e0 avail_size_in_buf=44459
[mjpeg @ 0x9793100] marker parser used 16 bytes (128 bits)
[mjpeg @ 0x9793100] marker=fe avail_size_in_buf=44441
[mjpeg @ 0x9793100] marker parser used 15 bytes (120 bits)
[mjpeg @ 0x9793100] marker=fe avail_size_in_buf=44424
[mjpeg @ 0x9793100] marker parser used 15 bytes (120 bits)
[mjpeg @ 0x9793100] marker=db avail_size_in_buf=44407
[mjpeg @ 0x9793100] index=0
[mjpeg @ 0x9793100] qscale[0]: 4
[mjpeg @ 0x9793100] marker parser used 67 bytes (536 bits)
[mjpeg @ 0x9793100] marker=db avail_size_in_buf=44338
[mjpeg @ 0x9793100] index=1
[mjpeg @ 0x9793100] qscale[1]: 9
[mjpeg @ 0x9793100] marker parser used 67 bytes (536 bits)
[mjpeg @ 0x9793100] marker=c0 avail_size_in_buf=44269
[mjpeg @ 0x9793100] sof0: picture: 800x600
[mjpeg @ 0x9793100] component 0 2:2 id: 0 quant:0
[mjpeg @ 0x9793100] component 1 1:1 id: 1 quant:1
[mjpeg @ 0x9793100] component 2 1:1 id: 2 quant:1
[mjpeg @ 0x9793100] pix fmt id 22111100
[mjpeg @ 0x9793100] marker parser used 17 bytes (136 bits)
[mjpeg @ 0x9793100] marker=c4 avail_size_in_buf=44250
[mjpeg @ 0x9793100] class=0 index=0 nb_codes=12
[mjpeg @ 0x9793100] marker parser used 31 bytes (248 bits)
[mjpeg @ 0x9793100] marker=c4 avail_size_in_buf=44217
[mjpeg @ 0x9793100] class=1 index=0 nb_codes=251
[mjpeg @ 0x9793100] marker parser used 181 bytes (1448 bits)
[mjpeg @ 0x9793100] marker=c4 avail_size_in_buf=44034
[mjpeg @ 0x9793100] class=0 index=1 nb_codes=12
[mjpeg @ 0x9793100] marker parser used 31 bytes (248 bits)
[mjpeg @ 0x9793100] marker=c4 avail_size_in_buf=44001
[mjpeg @ 0x9793100] class=1 index=1 nb_codes=251
[mjpeg @ 0x9793100] marker parser used 181 bytes (1448 bits)
[mjpeg @ 0x9793100] escaping removed 286 bytes
[mjpeg @ 0x9793100] marker=da avail_size_in_buf=43818
[mjpeg @ 0x9793100] component: 0
[mjpeg @ 0x9793100] component: 1
[mjpeg @ 0x9793100] component: 2
[mjpeg @ 0x9793100] marker parser used 43531 bytes (348244 bits)
[mjpeg @ 0x9793100] marker=d9 avail_size_in_buf=67
[mjpeg @ 0x9793100] decode frame unused 67 bytes
No more output streams to write to, finishing.
[AVIOContext @ 0x9d89e80] Statistics: 0 seeks, 1 writeouts
frame=    1 fps=0.0 q=0.0 Lsize=N/A time=00:00:00.04 bitrate=N/A
video:11kB audio:0kB subtitle:0 global headers:0kB muxing overhead
-100.194346%
1 frames successfully decoded, 0 decoding errors
[AVIOContext @ 0x9792da0] Statistics: 5013601 bytes read, 0 seeks

real 4m26.029s
user 0m0.216s
sys 0m0.112s


Four minutes later (?!) the frame is saved!

Guys, I really don't get that now!

-- 
Saravá,
Renato CRON
http://www.renatocron.com/blog/
@renato_cron <http://twitter.com/#!/renato_cron>


More information about the ffmpeg-user mailing list