任敬轩 3 rokov pred
rodič
commit
d24fc9ea19

+ 5 - 5
Modules/Camera/Http/Controllers/Api/HaiKangController.php

@@ -469,11 +469,11 @@ class HaiKangController extends BaseController
         }
 
         //更新拉流id
-        CameraList::where('id', $camera_id)->update(
-            [
-                'revert_back_id'  => trim($curl_res, '"'),
-            ]
-        );
+//        CameraList::where('id', $camera_id)->update(
+//            [
+//                'revert_back_id'  => trim($curl_res, '"'),
+//            ]
+//        );
 
         $result['data'] = [
             'camera_id' => Input::get('camera_id'),

+ 79 - 2
Modules/Camera/Services/CameraServices.php

@@ -1041,7 +1041,7 @@ class CameraServices
         $result_hk = $haikang->getPlaybackRtmp();
 
         if ($result_hk['data']['url']) {
-            $res = self::getVideoStream($result_hk['data']['url'], $path, $camera_id);
+            $res = self::getVideoBackStream($result_hk['data']['url'], $path, $camera_id);
             if($res != false){
                 $result['data'] = $res['data'];
             }else{//切换码流
@@ -1059,7 +1059,7 @@ class CameraServices
                 );
                 $result_hk = $haikang->getPlaybackRtmp();
                 if ($result_hk['data']['url']) {
-                    $res = self::getVideoStream($result_hk['data']['url'], $path, $camera_id);
+                    $res = self::getVideoBackStream($result_hk['data']['url'], $path, $camera_id);
                     if($res != false){
                         $result['data'] = $res['data'];
                     }else{
@@ -1795,6 +1795,83 @@ class CameraServices
         return $result;
     }
 
+    //回放获取视频流
+    public static function getVideoBackStream($video_url, $path, $camera_id)
+    {
+        $result = [];
+
+        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);
+        }
+
+//        $mine_list = DB::table('mine_list')
+//            ->select('mine_list.degree')
+//            ->leftJoin('camera_list','camera_list.mine_id','=','mine_list.id')
+//            ->where('camera_list.id',$camera_id)->get();
+//        if(isset($mine_list)){
+//            $mine_id = explode('|',$mine_list[0]->degree)[0];
+//            $mine_res = DB::table('mine_list')->where('id',$mine_id)->get();
+//        }
+//        //取流进程
+//        if(isset($mine_res[0]->title) && in_array($mine_res[0]->title,config('mine'))){
+//            $exec = 'nohup /usr/bin/ffmpeg -rtsp_transport tcp -i "' . $video_url . '" -b:v 200k -c:v libx264 -c:a copy -s 384*288 -vbsf h264_mp4toannexb -f hls -hls_flags delete_segments -segment_list_size 10 -hls_list_size 2 -threads 4 /www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path . '/' . CameraEnum::M3U8_FILE_NAME . ' >/dev/null &';
+//        }else{
+        $exec = 'nohup /usr/bin/ffmpeg -i "' . $video_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 &';
+//        }
+
+        //取流进程
+//        $exec = 'nohup /usr/bin/ffmpeg -i "' . $video_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 &';
+
+//        $exec = 'nohup /usr/bin/ffmpeg -rtsp_transport tcp -i "' . $video_url . '" -b:v 200k -c:v libx264 -c:a copy -s 384*288 -vbsf h264_mp4toannexb -f hls -hls_flags delete_segments -segment_list_size 10 -hls_list_size 2 -threads 4 /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);
+
+            //查询是否存在封面图
+            $cover_picture = CameraList::where('id', $camera_id)->value('cover_picture');
+
+            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) {
+                        //上传图片到oss并更新数据库
+                        $picture_path = self::uploadOssFile($file_name, public_path() . '/' . CameraEnum::M3U8_FILE_PATH . '/' . $path . '/');
+                        CameraList::where('id', $camera_id)->update(['cover_picture' => $picture_path]);
+                    }
+                }
+
+                $result['data'] = [
+                    'camera_id' => $camera_id,
+                    'url' => env('VIDEO_SYSTEM_URL') . CameraEnum::M3U8_FILE_PATH . '/' . $path . '/' . CameraEnum::M3U8_FILE_NAME . '?' . time(),
+                ];
+
+//                CameraList::where('id', $camera_id)->update(['revert_id' => CameraEnum::CAMERA_FILE_EXIST]);
+
+                return $result;
+            } else {
+                sleep(1);
+            }
+
+            $i++;
+        }
+
+        return false;
+    }
+
     //获取视频流
     public static function getVideoStream($video_url, $path, $camera_id)
     {