Flutter M3U8视频下载插件m3u8_download的使用
Flutter M3U8视频下载插件m3u8_download的使用
为什么有这个项目
我想下载一些视频,但是使用 ffmpeg -i xxx -c copy xxx.mp4
太慢了。
因为单线程下载似乎有一个速度限制,整体速度非常慢,并且在线播放会卡顿。但多线程下载不会太慢。所以我编写了这个工具,它可以解析 m3u8 文件并以多线程方式下载 ts 文件,并在本地完成“合并”工作以加快速度。
需求
该可执行文件适用于 Windows、Linux 和 macOS。
并且,你需要先安装 ffmpeg。
安装
dart pub global activate m3u8_download
git clone https://github.com/CaiJingLong/m3u8_download.git
cd m3u8_download
dart pub global activate -s path .
或者,从 发布页面 下载
# 下载后
chmod +x m3u8
使用
pub global
m3u8 -u xxx -o download
使用源代码
dart bin/m3u8_download.dart -u xxx
命令的帮助信息
$ m3u8 -h
Download m3u8 and merge to video(mp4)
Usage: m3u8_download <command> [arguments]
Global options:
-u, --url m3u8 url
-o, --output output file name (not have ext)
(defaults to "download/video")
-p, --protocol supported protocol (for ffmpeg merge)
(defaults to "file,crypto,data,http,tcp,https,tls")
-e, --ext output file ext.
(defaults to "mp4")
-t, --threads download threads
(defaults to "20")
-v, --[no-]verbose show verbose log
-r, --[no-]remove-temp remove temp file after merge
(defaults to on)
-h, --help Print this usage information.
Available commands:
help Display help information for m3u8_download.
Run "m3u8_download help <command>" for more information about a command.
Example: m3u8 -u http://example.com/index.m3u8 -o download
完整示例Demo
以下是一个完整的示例,展示如何使用 m3u8_download
插件来下载和合并 m3u8 视频。
首先,确保你已经安装了 ffmpeg
并激活了 m3u8_download
插件:
# 安装 ffmpeg
sudo apt-get install ffmpeg # 在 Debian/Ubuntu 上
brew install ffmpeg # 在 macOS 上
# 激活 m3u8_download
dart pub global activate m3u8_download
接下来,你可以使用以下命令来下载和合并 m3u8 视频:
# 使用 pub global
m3u8 -u http://example.com/index.m3u8 -o my_video
# 或者使用源代码
dart bin/m3u8_download.dart -u http://example.com/index.m3u8 -o my_video
这将下载 http://example.com/index.m3u8
中的所有片段,并将它们合并成一个名为 my_video.mp4
的视频文件。
如果你希望看到详细的日志输出,可以添加 -v
参数:
m3u8 -u http://example.com/index.m3u8 -o my_video -v
如果你想删除临时文件,可以添加 --remove-temp
参数:
m3u8 -u http://example.com/index.m3u8 -o my_video --remove-temp
更多关于Flutter M3U8视频下载插件m3u8_download的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter M3U8视频下载插件m3u8_download的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter中使用m3u8_download
插件来下载M3U8视频的示例代码。这个插件允许你下载并管理M3U8格式的视频流。
首先,你需要在你的pubspec.yaml
文件中添加这个插件的依赖:
dependencies:
flutter:
sdk: flutter
m3u8_download: ^x.y.z # 请替换为最新的版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,你可以使用以下代码来下载M3U8视频:
import 'package:flutter/material.dart';
import 'package:m3u8_download/m3u8_download.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter M3U8 Download Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DownloadPage(),
);
}
}
class DownloadPage extends StatefulWidget {
@override
_DownloadPageState createState() => _DownloadPageState();
}
class _DownloadPageState extends State<DownloadPage> {
String _downloadStatus = "Not Started";
void startDownload() async {
// M3U8 URL
String m3u8Url = "http://example.com/path/to/your/playlist.m3u8";
// Destination directory (e.g., downloads directory)
final dir = await getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
final filePath = "${dir.path}/downloaded_video.mp4";
// Initialize the download
final downloadId = await M3U8Download.startDownload(
url: m3u8Url,
saveDir: dir.path,
fileName: "downloaded_video.mp4",
headers: null, // Optionally, add headers if needed
isLive: false, // Set to true if it's a live stream
);
// Update state to reflect the download status
setState(() {
_downloadStatus = "Downloading... (ID: $downloadId)";
});
// Optionally, listen to download progress updates
M3U8Download.downloadProgress(downloadId).listen((progress) {
print("Download Progress: ${progress.progress}%");
// Update UI or handle progress as needed
});
// Optionally, listen to download completion or error
M3U8Download.downloadStatus(downloadId).listen((status) {
if (status.state == DownloadState.COMPLETED) {
setState(() {
_downloadStatus = "Download Completed: ${status.filePath}";
});
} else if (status.state == DownloadState.ERROR) {
setState(() {
_downloadStatus = "Download Failed: ${status.error?.message}";
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('M3U8 Download Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Download Status:',
style: TextStyle(fontSize: 20),
),
Text(
_downloadStatus,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: startDownload,
child: Text('Start Download'),
),
],
),
),
);
}
}
注意事项
-
权限:确保你的应用有读写外部存储的权限。在Android上,你需要在
AndroidManifest.xml
中添加以下权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
并在运行时请求这些权限(使用
permission_handler
插件或其他方法)。 -
iOS权限:在iOS上,你需要在
Info.plist
中添加对NSAppTransportSecurity的例外处理,以允许HTTP请求(如果M3U8 URL是HTTP的)。 -
错误处理:在实际应用中,应该添加更多的错误处理和UI更新逻辑,以处理各种可能的异常情况。
这个示例展示了如何使用m3u8_download
插件来启动M3U8视频的下载,并监听下载进度和状态更新。你可以根据需要进一步定制和扩展这个示例。