浏览代码

firstcommit

Tum 3 年之前
当前提交
0032e20e2d
共有 19 个文件被更改,包括 174 次插入0 次删除
  1. 5 0
      .gitignore
  2. 11 0
      .videoscctv.m3u8
  3. 二进制
      .videoscctv0.ts
  4. 二进制
      .videoscctv1.ts
  5. 二进制
      .videoscctv2.ts
  6. 二进制
      .videoscctv3.ts
  7. 二进制
      .videoscctv4.ts
  8. 二进制
      .videoscctv5.ts
  9. 二进制
      .videoscctv6.ts
  10. 二进制
      .videoscctv7.ts
  11. 二进制
      .videoscctv8.ts
  12. 10 0
      README.md
  13. 66 0
      app.py
  14. 17 0
      m3u8_convert
  15. 21 0
      pages/index.html
  16. 6 0
      requirements.txt
  17. 4 0
      run.sh
  18. 二进制
      static/frame.jpg
  19. 34 0
      templates/index.html

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
1
+__pycache__/
2
+env/
3
+.idea/
4
+.DS_Store
5
+video/

+ 11 - 0
.videoscctv.m3u8

@@ -0,0 +1,11 @@
1
+#EXTM3U
2
+#EXT-X-VERSION:3
3
+#EXT-X-TARGETDURATION:6
4
+#EXT-X-MEDIA-SEQUENCE:6
5
+#EXTINF:6.001522,
6
+.videoscctv6.ts
7
+#EXTINF:4.004278,
8
+.videoscctv7.ts
9
+#EXTINF:4.120000,
10
+.videoscctv8.ts
11
+#EXT-X-ENDLIST

二进制
.videoscctv0.ts


二进制
.videoscctv1.ts


二进制
.videoscctv2.ts


二进制
.videoscctv3.ts


二进制
.videoscctv4.ts


二进制
.videoscctv5.ts


二进制
.videoscctv6.ts


二进制
.videoscctv7.ts


二进制
.videoscctv8.ts


+ 10 - 0
README.md

@@ -0,0 +1,10 @@
1
+Simple Flask HLS Demo
2
+
3
+How to run
4
+
5
+~~~bash
6
+pip install -r requirements.txt
7
+./run.sh
8
+~~~
9
+
10
+go [http://localhost:5000](http://localhost:5000)

+ 66 - 0
app.py

@@ -0,0 +1,66 @@
1
+from flask import render_template, Flask, send_from_directory, Response
2
+import cv2
3
+
4
+app = Flask(__name__)
5
+
6
+
7
+@app.after_request
8
+def add_header(response):
9
+    response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
10
+    response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
11
+    response.headers['Pragma'] = 'no-cache'
12
+    response.headers['Expires'] = '0'
13
+    return response
14
+
15
+
16
+def find_camera(id):
17
+    '''
18
+    cameras = ['rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp',
19
+    'rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp']
20
+    '''
21
+    cameras = ['rtsp://admin:@Unv123456@192.168.10.252:554/unicast/c1/s1/live']
22
+    return cameras[int(id)]
23
+#  for cctv camera use rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp' instead of camera
24
+#  for webcam use zero(0)
25
+
26
+
27
+def gen_frames(camera_id):
28
+
29
+    cam = find_camera(camera_id)
30
+    cap=  cv2.VideoCapture(cam)
31
+
32
+    while True:
33
+        # for cap in caps:
34
+        # # Capture frame-by-frame
35
+        success, frame = cap.read()  # read the camera frame
36
+        if not success:
37
+            break
38
+        else:
39
+            ret, buffer = cv2.imencode('.jpg', frame)
40
+            frame = buffer.tobytes()
41
+            yield (b'--frame\r\n'
42
+                b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')  # concat frame one by one and show result
43
+
44
+
45
+
46
+@app.route('/video_feed/<string:id>/', methods=["GET"])
47
+def video_feed(id):
48
+
49
+    """Video streaming route. Put this in the src attribute of an img tag."""
50
+    return Response(gen_frames(id),
51
+                    mimetype='multipart/x-mixed-replace; boundary=frame')
52
+
53
+
54
+@app.route('/')
55
+def index():
56
+    return render_template('index.html')
57
+
58
+
59
+@app.route('/video/<string:file_name>')
60
+def stream(file_name):
61
+    video_dir = './video'
62
+    return send_from_directory(video_dir, file_name)
63
+
64
+
65
+if __name__ == '__main__':
66
+    app.run(debug=True)

+ 17 - 0
m3u8_convert

@@ -0,0 +1,17 @@
1
+ffmpeg -fflags nobuffer \
2
+ -rtsp_transport tcp \
3
+ -i "rtsp://admin:@Unv123456@192.168.10.252:554/unicast/c1/s1/live" \
4
+ -vsync 0 \
5
+ -copyts \
6
+ -vcodec libx264 \
7
+ -movflags frag_keyframe+empty_moov \
8
+ -acodec aac \
9
+ -hls_flags delete_segments+append_list \
10
+ -f segment \
11
+ -segment_list_flags live \
12
+ -segment_time 0.5 \
13
+ -segment_list_size 10 \
14
+ -segment_format mpegts \
15
+ -segment_list video/cctv.m3u8 \
16
+ -segment_list_type m3u8 \
17
+ video/cctv%3d.ts

+ 21 - 0
pages/index.html

@@ -0,0 +1,21 @@
1
+<!DOCTYPE html>
2
+<html>
3
+   <head>
4
+      <meta charset="utf-8">
5
+      <title>Test</title>
6
+      <link href="https://vjs.zencdn.net/6.2.8/video-js.css" rel="stylesheet">
7
+   </head>
8
+   <body>
9
+      <video id="player" class="video-js vjs-default-skin" controls preload>
10
+         <source src="./video/playlist.m3u8" type="application/x-mpegURL">
11
+      </video>
12
+      <script src="https://vjs.zencdn.net/6.2.8/video.js"></script>
13
+      <script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.12.2/videojs-contrib-hls.min.js"></script>
14
+      <script>
15
+        (function(){
16
+          var player = videojs('player', {width: 350, height: 200});
17
+          player.play();
18
+        })();
19
+     </script>
20
+   </body>
21
+</html>

+ 6 - 0
requirements.txt

@@ -0,0 +1,6 @@
1
+click==6.7
2
+flask>=0.12.3
3
+itsdangerous==0.24
4
+Jinja2==2.10.1
5
+MarkupSafe==1.0
6
+Werkzeug==0.15.3

+ 4 - 0
run.sh

@@ -0,0 +1,4 @@
1
+#!/usr/bin/env bash
2
+
3
+export FLASK_APP=app.py
4
+flask run --no-reload --host=0.0.0.0

二进制
static/frame.jpg


+ 34 - 0
templates/index.html

@@ -0,0 +1,34 @@
1
+<!DOCTYPE html>
2
+<html>
3
+   <head>
4
+      <meta charset="utf-8">
5
+      <title>Test</title>
6
+      <link href="http://vjs.zencdn.net/6.2.8/video-js.css" rel="stylesheet">
7
+    <meta charset="utf-8">
8
+    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
9
+
10
+    <!-- Bootstrap CSS -->
11
+    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
12
+          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
13
+   </head>
14
+   <body>
15
+       <div class='container'>
16
+           <div class='row'>
17
+               <div class='col-md-6'>
18
+                   <video id="player" class="video-js vjs-default-skin" controls preload>
19
+                       <source src="./video/cctv.m3u8" type="application/x-mpegURL">
20
+                   </video>
21
+               </div>
22
+               <div class='col-md-6'>
23
+                   <img src="http://localhost:9099/video_feed/0/" width="100%">
24
+               </div>
25
+           </div>
26
+       </div>
27
+    <script src="https://vjs.zencdn.net/7.20.3/video.min.js"></script>
28
+      <script src="https://unpkg.com/@videojs/http-streaming@2.15.1/dist/videojs-http-streaming.js"></script>
29
+      <script>
30
+          var player = videojs('player', {width: 480, height:270 });
31
+          player.play();
32
+     </script>
33
+   </body>
34
+</html>