@@ -2,13 +2,28 @@ import 'package:bmff/bmff.dart';
22import 'package:http/http.dart' as http;
33
44Future <void > main (List <String > args) async {
5- // final uri = Uri.parse('https://www.w3school.com.cn/i/movie.mp4');
6- final uri = Uri .parse (
7- 'http://vfx.mtime.cn/Video/2019/03/18/mp4/190318231014076505.mp4' );
5+ final urls = [
6+ 'http://zuoye.free.fr/files/compare_still_1.heic.jpg' ,
7+ // 'http://vfx.mtime.cn/Video/2019/03/18/mp4/190318231014076505.mp4',
8+ // 'https://www.w3school.com.cn/i/movie.mp4',
9+ // 'https://cdn.jsdelivr.net/gh/ExampleAssets/ExampleAsset@master/preview_0.heic',
10+ ];
11+
12+ for (final url in urls) {
13+ await showHttpBoxInfo (url);
14+ }
15+ }
16+
17+ Future <void > showHttpBoxInfo (String url) async {
18+ print ('------------------ $url ------------------' );
19+
20+ final uri = Uri .parse (url);
821 final bmff = await Bmff .asyncContext (AsyncBmffContextHttp (uri));
922 for (final box in bmff.childBoxes) {
1023 showBoxInfo (0 , box);
1124 }
25+
26+ print ('\n ' );
1227}
1328
1429void showBoxInfo (int level, AsyncBmffBox box) {
@@ -23,30 +38,53 @@ void showBoxInfo(int level, AsyncBmffBox box) {
2338// part download http context
2439class AsyncBmffContextHttp extends AsyncBmffContext {
2540 final Uri uri;
41+ final int maxCacheLength;
42+
43+ AsyncBmffContextHttp (
44+ this .uri, {
45+ this .maxCacheLength = 1024 * 1024 * 1 ,
46+ });
2647
27- const AsyncBmffContextHttp ( this .uri) ;
48+ List < int > ? _bytes ;
2849
2950 @override
3051 Future <List <int >> getRangeData (int start, int end) async {
52+ if (_bytes != null ) {
53+ return _bytes! .sublist (start, end);
54+ }
55+
3156 final response = await http.get (uri, headers: {
3257 'Range' : 'bytes=$start -${end - 1 }' ,
3358 });
3459 if (response.statusCode != 206 ) {
3560 throw Exception (
36- 'Current http status code is ${response .statusCode }, not 206, not support range download' );
61+ 'Current http status code is ${response .statusCode }, not 206. '
62+ 'The server not support range download' );
3763 }
3864 final bytes = response.bodyBytes;
3965 return bytes;
4066 }
4167
4268 @override
4369 Future <int > lengthAsync () async {
44- final response = await http.head (uri);
45- final contentLength = response.headers['content-length' ];
46- if (contentLength != null ) {
47- return int .parse (contentLength);
70+ final response = await http.head (uri, headers: {
71+ 'User-Agent' : 'curl/7.79.1' ,
72+ 'Accept' : '*/*' ,
73+ 'Host' : uri.host,
74+ });
75+ final contentLengthList = response.headers.entries
76+ .where ((element) => element.key.toLowerCase () == 'content-length' );
77+ if (contentLengthList.isNotEmpty) {
78+ final contentLength = int .parse (contentLengthList.first.value);
79+ if (contentLength < maxCacheLength) {
80+ final response = await http.get (uri);
81+ _bytes = response.bodyBytes;
82+ }
83+ return contentLength;
4884 } else {
49- throw Exception ('content-length not found' );
85+ final response = await http.get (uri);
86+ _bytes = response.bodyBytes;
87+ return response.bodyBytes.length;
5088 }
5189 }
5290}
0 commit comments