Преглед на файлове

金家渠保存首帧图片

任敬轩 преди 3 години
родител
ревизия
e0c662585a
променени са 2 файла, в които са добавени 78 реда и са изтрити 10 реда
  1. 13 9
      Modules/Camera/Http/Controllers/Api/HaiKangController.php
  2. 65 1
      Modules/Camera/Services/CameraServices.php

+ 13 - 9
Modules/Camera/Http/Controllers/Api/HaiKangController.php

@@ -422,13 +422,7 @@ class HaiKangController extends BaseController
             }
         }
 
-        if (!is_dir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path)) {
-            mkdir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path, 0777, true);
-        }
-        // dd($result_td['content'][0]['url']);
-        $exec = 'nohup /usr/bin/ffmpeg -i "' . $rtsp['data']['url'] . '" -vcodec copy -acodec copy -vbsf h264_mp4toannexb -f hls -hls_flags delete_segments -segment_list_size 10 -hls_list_size 2 /www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path . '/' . CameraEnum::M3U8_FILE_NAME . ' >/dev/null &';
 
-        shell_exec($exec);
 
 //        CameraList::where('id', Input::get('camera_id'))->update(['revert_id' => $curl_res]);
 
@@ -437,6 +431,19 @@ class HaiKangController extends BaseController
             'url' => env('EASY_DARWIN_JF_RTSP') . $path
         ];
 
+        //查询是否存在封面图
+        $cover_picture = CameraList::where('id', Input::get('camera_id'))->value('cover_picture');
+
+        if (!$cover_picture) {
+            if (!is_dir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path)) {
+                mkdir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path, 0777, true);
+            }
+            // dd($result_td['content'][0]['url']);
+            $exec = 'nohup /usr/bin/ffmpeg -i "' . $rtsp['data']['url'] . '" -vcodec copy -acodec copy -vbsf h264_mp4toannexb -f hls -hls_flags delete_segments -segment_list_size 10 -hls_list_size 2 /www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path . '/' . CameraEnum::M3U8_FILE_NAME . ' >/dev/null &';
+
+            shell_exec($exec);
+        }
+
         //sleep10秒生成文件
         $i = 1;
         while ($i <= 10) {
@@ -445,11 +452,8 @@ class HaiKangController extends BaseController
 
             //判断ts文件是否存在
             $ts_exists = glob(public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . CameraEnum::TS_FILE_SUFFIX);
-            //查询是否存在封面图
-            $cover_picture = CameraList::where('id', Input::get('camera_id'))->value('cover_picture');
 
             if ($file_exists && $ts_exists) {
-                //查询是否存在封面图
 
                 if (!$cover_picture) {
                     //获取第一帧作为封面图

+ 65 - 1
Modules/Camera/Services/CameraServices.php

@@ -574,13 +574,77 @@ class CameraServices
             return $result;
         }
 
-        CameraList::where('id', $camera_id)->update(['revert_id' => $curl_res]);
+//        CameraList::where('id', $camera_id)->update(['revert_id' => $curl_res]);
 
         $result['data'] = [
             'camera_id' => $camera_id,
             'url' => env('EASY_DARWIN_JF_RTSP') . $path
         ];
 
+        //查询是否存在封面图
+        $cover_picture = CameraList::where('id', Input::get('camera_id'))->value('cover_picture');
+
+        if (!$cover_picture) {
+            if (!is_dir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path)) {
+                mkdir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path, 0777, true);
+            }
+            // dd($result_td['content'][0]['url']);
+            $exec = 'nohup /usr/bin/ffmpeg -i "' . $result_td['content'][0]['url'] . '" -vcodec copy -acodec copy -vbsf h264_mp4toannexb -f hls -hls_flags delete_segments -segment_list_size 10 -hls_list_size 2 /www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path . '/' . CameraEnum::M3U8_FILE_NAME . ' >/dev/null &';
+
+            shell_exec($exec);
+        }
+
+        //sleep10秒生成文件
+        $i = 1;
+        while ($i <= 10) {
+            //判断m3u8文件是否存在
+            $file_exists = file_exists(public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path . '/' . CameraEnum::M3U8_FILE_NAME);
+
+            //判断ts文件是否存在
+            $ts_exists = glob(public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . CameraEnum::TS_FILE_SUFFIX);
+
+            if ($file_exists && $ts_exists) {
+
+                if (!$cover_picture) {
+                    //获取第一帧作为封面图
+                    $file_name = time() . CameraEnum::COVER_PICTURE_NAME;
+
+                    $cover_exec = 'ffmpeg -i ' . public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . CameraEnum::M3U8_FILE_NAME . ' -vf "select=between(mod(n\, 25)\, 0\, 0), setpts=N/24/TB" ' . public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . $file_name;
+                    exec($cover_exec);
+
+                    //判断图片是否存在
+                    $picture_exists = file_exists(public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . $file_name);
+
+                    if ($picture_exists) {
+                        $cms = new CameraServices();
+
+                        //上传图片到oss并更新数据库
+                        $picture_path = $cms->uploadOssFile($file_name, public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/');
+
+                        CameraList::where('id', $camera_id)->update(['cover_picture' => $picture_path]);
+                    }
+                }
+
+                //更新拉流id
+                CameraList::where('id', $camera_id)->update(
+                    [
+                        'revert_id'  => trim($curl_res, '"'),
+                        'updated_at' => date('Y-m-d H:i:s'),
+                    ]
+                );
+                return $result;
+            } else {
+                sleep(1);
+            }
+
+            $i++;
+        }
+
+        //标记摄像头为异常状态
+        CameraList::where('id', Input::get('camera_id'))->update(['camera_status' => CameraEnum::CAMERA_STATUS_ERROR]);
+
+        $result['status'] = false;
+        $result['msg']    = ApiEnum::EASY_DAWIN_REQUEST_FAIL;
 
         return $result;
     }