From 7801467e3602f90785dbc9a7edfbf07608735011 Mon Sep 17 00:00:00 2001 From: renlu Date: Fri, 2 Dec 2022 11:01:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E5=90=84=E4=BD=8D=E4=B8=BAUFT-8=20BOM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 69 +++++++++++++++++++------------------- src/Common/MediaSource.cpp | 2 +- src/Common/MediaSource.h | 2 +- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 61887a65..319a222d 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). @@ -1485,44 +1485,43 @@ void installWebApi() { auto scan_path = File::absolutePath(MD5(allArgs["url"]).hexdigest(), snap_root) + "/"; string new_snap = StrPrinter << scan_path << time(NULL) << ".jpeg"; - if (expire_sec != 0){ - File::scanDir(scan_path, [&](const string &path, bool isDir) { - if (isDir || !end_with(path, ".jpeg")) { - //忽略文件夹或其他类型的文件 - return true; - } - - //找到截图 - auto tm = FindField(path.data() + scan_path.size(), nullptr, ".jpeg"); - if (atoll(tm.data()) + expire_sec < time(NULL)) { - //截图已经过期,改名,以便再次请求时,可以返回老截图 - rename(path.data(), new_snap.data()); - have_old_snap = true; - return true; - } - - //截图存在,且未过期,那么返回之 - res_old_snap = true; - responseSnap(path, allArgs.getParser().getHeader(), invoker); - //中断遍历 - return false; - }); - - if (res_old_snap) { - //已经回复了旧的截图 - return; + File::scanDir(scan_path, [&](const string &path, bool isDir) { + if (isDir || !end_with(path, ".jpeg")) { + //忽略文件夹或其他类型的文件 + return true; } - //无截图或者截图已经过期 - if (!have_old_snap) { - //无过期截图,生成一个空文件,目的是顺便创建文件夹路径 - //同时防止在FFmpeg生成截图途中不停的尝试调用该api多次启动FFmpeg进程 - auto file = File::create_file(new_snap.data(), "wb"); - if (file) { - fclose(file); - } + //找到截图 + auto tm = FindField(path.data() + scan_path.size(), nullptr, ".jpeg"); + if (atoll(tm.data()) + expire_sec < time(NULL)) { + //截图已经过期,改名,以便再次请求时,可以返回老截图 + rename(path.data(), new_snap.data()); + have_old_snap = true; + return true; + } + + //截图存在,且未过期,那么返回之 + res_old_snap = true; + responseSnap(path, allArgs.getParser().getHeader(), invoker); + //中断遍历 + return false; + }); + + if (res_old_snap) { + //已经回复了旧的截图 + return; + } + + //无截图或者截图已经过期 + if (!have_old_snap) { + //无过期截图,生成一个空文件,目的是顺便创建文件夹路径 + //同时防止在FFmpeg生成截图途中不停的尝试调用该api多次启动FFmpeg进程 + auto file = File::create_file(new_snap.data(), "wb"); + if (file) { + fclose(file); } } + //启动FFmpeg进程,开始截图,生成临时文件,截图成功后替换为正式文件 auto new_snap_tmp = new_snap + ".tmp"; FFmpegSnap::makeSnap(allArgs["url"], new_snap_tmp, allArgs["timeout_sec"], [invoker, allArgs, new_snap, new_snap_tmp](bool success, const string &err_msg) { diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 1144f110..5e378124 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index f8510538..84961d2c 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).