# video_downloader **Repository Path**: yangyxd/video_downloader ## Basic Information - **Project Name**: video_downloader - **Description**: 基于 ffmpeg 实现视频下载和转码为 mp4 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-09-21 - **Last Updated**: 2022-09-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # x64 视频下载器 使用 ffmpeg 进行视频下载。可以显示下载进度。 video_down.dll 可以中实现了具体的功能,可供第三方使用(如 flutter ) ## FFMPEG 下载 - [蓝奏云 ffmpeg.exe 下载](https://eso.lanzoum.com/iOa8M0c1586d) - [官方镜像 www.gyan.dev](https://www.gyan.dev/ffmpeg/builds/) ## Flutter 中使用 video_down.dll 示例 使用 FFI 方式调用 video_down.dll ```dart ////// ffmpeg 处理 typedef _DownloadM3U8N = Uint64 Function( Pointer url, Pointer outFile, Pointer onState, Uint32 timeout); typedef _DownloadM3U8D = int Function( Pointer url, Pointer outFile, Pointer onState, int timeout); typedef _StopDownloadM3U8N = Void Function(Uint64 handle); typedef _StopDownloadM3U8D = void Function(int handle); typedef _GetDownloadM3U8StatusN = Int32 Function(Uint64 handle, Pointer recv, Pointer total); typedef _GetDownloadM3U8StatusD = int Function(int handle, Pointer recv, Pointer total); class FFMpegWin { static DynamicLibrary? _library; static Pointer 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 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 url = indexFile.toNativeUtf16(); final Pointer out = outFile.toNativeUtf16(); final Pointer recv = ffi.malloc.call(); final Pointer total = ffi.malloc.call(); 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; } } ```