代码拉取完成,页面将自动刷新
使用 ffmpeg 进行视频下载。可以显示下载进度。
video_down.dll 可以中实现了具体的功能,可供第三方使用(如 flutter )
使用 FFI 方式调用 video_down.dll
////// ffmpeg 处理
typedef _DownloadM3U8N = Uint64 Function(
Pointer<ffi.Utf16> url, Pointer<ffi.Utf16> outFile, Pointer onState, Uint32 timeout);
typedef _DownloadM3U8D = int Function(
Pointer<ffi.Utf16> url, Pointer<ffi.Utf16> outFile, Pointer onState, int timeout);
typedef _StopDownloadM3U8N = Void Function(Uint64 handle);
typedef _StopDownloadM3U8D = void Function(int handle);
typedef _GetDownloadM3U8StatusN = Int32 Function(Uint64 handle, Pointer<Uint32> recv, Pointer<Uint32> total);
typedef _GetDownloadM3U8StatusD = int Function(int handle, Pointer<Uint32> recv, Pointer<Uint32> total);
class FFMpegWin {
static DynamicLibrary? _library;
static Pointer<void> nil = Pointer.fromAddress(0);
static void windowsInit() {
if (_library != null) return;
const dll = "video_down.dll";
try {
_library = DynamicLibrary.open(dll);
} catch (e) {
stderr.writeln('Failed to load $dll');
rethrow;
}
}
/// 转换下载好的本地 m3u8 文件为 mp4
///
/// 处理结果:-1 任务不存在, 0 初始化中, 1 下载中,2 下载成功完成,3 下载超时结束,4 未知原因下载失败
static Future<int> localM3u8ToMP4(String indexFile, String outFile) async {
if (Platform.isWindows) {
windowsInit();
} else {
return -1;
}
final downloadM3U8 = _library!.lookupFunction<_DownloadM3U8N, _DownloadM3U8D>("DownloadM3U8");
final getDownState = _library!.lookupFunction<_GetDownloadM3U8StatusN, _GetDownloadM3U8StatusD>("GetDownloadM3U8Status");
// final stopDownloadM3U8 = _library!.lookupFunction<_StopDownloadM3U8N, _StopDownloadM3U8D>("StopDownloadM3U8");
final Pointer<ffi.Utf16> url = indexFile.toNativeUtf16();
final Pointer<ffi.Utf16> out = outFile.toNativeUtf16();
final Pointer<Uint32> recv = ffi.malloc.call<Uint32>();
final Pointer<Uint32> total = ffi.malloc.call<Uint32>();
int result = 0;
try {
final handle = downloadM3U8(url, out, nil as dynamic, 60000);
if (handle != 0) {
while (true) {
await Utils.sleep(300);
// 获取下载状态: -1 任务不存在, 0 初始化中, 1 下载中,2 下载成功完成,3 下载超时结束,4 未知原因下载失败
result = getDownState(handle, recv, total);
if (result > 1 || result == -1) break;
}
}
} finally {
ffi.malloc.free(url);
ffi.malloc.free(out);
ffi.malloc.free(recv);
ffi.malloc.free(total);
}
return result;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。