dedlepexa commited on
Commit
003a809
·
verified ·
1 Parent(s): c879e14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -91
app.py CHANGED
@@ -1,129 +1,106 @@
1
  from fastapi import FastAPI
2
- from fastapi.responses import PlainTextResponse
3
- from fastapi.staticfiles import StaticFiles
4
- import os
5
  import cv2
6
- import yt_dlp
7
- import shutil
 
8
 
9
  app = FastAPI()
10
 
11
- FRAMES_DIR = "frames"
12
- os.makedirs(FRAMES_DIR, exist_ok=True)
13
-
14
- app.mount("/frames", StaticFiles(directory=FRAMES_DIR), name="frames")
15
 
16
- current_frame = 0
17
- total_frames = 0
18
 
19
- MAPS_X = 2
20
- MAPS_Y = 3
21
 
22
- TILE = 128
23
 
24
- VIDEO_W = MAPS_X * TILE
25
- VIDEO_H = MAPS_Y * TILE
26
 
 
27
 
28
- def process_video(video_path):
 
 
 
 
 
 
 
29
 
30
- global total_frames, current_frame
31
 
32
- shutil.rmtree(FRAMES_DIR, ignore_errors=True)
33
- os.makedirs(FRAMES_DIR)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- cap = cv2.VideoCapture(video_path)
36
-
37
- frame_id = 0
38
- read_id = 0
39
 
40
  while True:
41
 
42
- ret, frame = cap.read()
43
- if not ret:
44
- break
45
-
46
- # берём только каждый 90 кадр
47
- if read_id % 90 != 0:
48
- read_id += 1
49
- continue
50
 
51
- frame = cv2.resize(frame, (VIDEO_W, VIDEO_H))
 
52
 
53
- map_id = 0
 
 
 
 
 
54
 
55
- for y in range(MAPS_Y):
56
- for x in range(MAPS_X):
57
 
58
- part = frame[
59
- y*TILE:(y+1)*TILE,
60
- x*TILE:(x+1)*TILE
61
  ]
62
 
63
- filename = f"{FRAMES_DIR}/frame_{frame_id}_map{map_id}.png"
64
-
65
- cv2.imwrite(filename, part)
66
-
67
- map_id += 1
68
-
69
- frame_id += 1
70
- read_id += 1
71
-
72
- if frame_id > 300:
73
- break
74
 
75
- cap.release()
76
 
77
- total_frames = frame_id
78
- current_frame = 0
79
 
80
 
81
  @app.get("/youtube")
82
- async def youtube(url: str):
83
-
84
- ydl_opts = {
85
- "outtmpl": "video.mp4",
86
- "format": "mp4"
87
- }
88
-
89
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
90
- ydl.download([url])
91
-
92
- process_video("video.mp4")
93
-
94
- return {"status": "video loaded", "frames": total_frames}
95
-
96
-
97
- @app.get("/frame", response_class=PlainTextResponse)
98
- async def frame():
99
-
100
- if total_frames == 0:
101
- return "no video"
102
-
103
- lines = []
104
-
105
- for i in range(6):
106
- lines.append(f"/frames/frame_{current_frame}_map{i}.png")
107
-
108
- return "\n".join(lines)
109
-
110
 
111
- @app.get("/next")
112
- async def next_frame():
 
 
113
 
114
- global current_frame
115
 
116
- if total_frames == 0:
117
- return {"error": "no video"}
118
 
119
- current_frame += 1
120
 
121
- if current_frame >= total_frames:
122
- current_frame = 0
123
 
124
- return {"frame": current_frame}
125
 
 
 
126
 
 
127
  if __name__ == "__main__":
128
- import uvicorn
129
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  from fastapi import FastAPI
 
 
 
2
  import cv2
3
+ import os
4
+ import subprocess
5
+ import threading
6
 
7
  app = FastAPI()
8
 
9
+ FRAME_FOLDER = "frames"
10
+ MAP_W = 3
11
+ MAP_H = 2
 
12
 
13
+ frame_index = 0
 
14
 
15
+ os.makedirs(FRAME_FOLDER, exist_ok=True)
 
16
 
 
17
 
18
+ def stream_youtube(url):
 
19
 
20
+ global frame_index
21
 
22
+ command = [
23
+ "yt-dlp",
24
+ "-f",
25
+ "best",
26
+ "-o",
27
+ "-",
28
+ url
29
+ ]
30
 
31
+ ytdlp = subprocess.Popen(command, stdout=subprocess.PIPE)
32
 
33
+ ffmpeg = subprocess.Popen(
34
+ [
35
+ "ffmpeg",
36
+ "-i",
37
+ "pipe:0",
38
+ "-vf",
39
+ "scale=384:256",
40
+ "-f",
41
+ "image2pipe",
42
+ "-pix_fmt",
43
+ "bgr24",
44
+ "-vcodec",
45
+ "rawvideo",
46
+ "-"
47
+ ],
48
+ stdin=ytdlp.stdout,
49
+ stdout=subprocess.PIPE
50
+ )
51
 
52
+ frame_size = 384 * 256 * 3
 
 
 
53
 
54
  while True:
55
 
56
+ raw = ffmpeg.stdout.read(frame_size)
 
 
 
 
 
 
 
57
 
58
+ if not raw:
59
+ break
60
 
61
+ frame = (
62
+ cv2
63
+ .numpy
64
+ .frombuffer(raw, dtype='uint8')
65
+ .reshape((256, 384, 3))
66
+ )
67
 
68
+ for y in range(MAP_H):
69
+ for x in range(MAP_W):
70
 
71
+ tile = frame[
72
+ y*128:(y+1)*128,
73
+ x*128:(x+1)*128
74
  ]
75
 
76
+ path = f"{FRAME_FOLDER}/frame_{frame_index}_map{y*MAP_W+x}.png"
 
 
 
 
 
 
 
 
 
 
77
 
78
+ cv2.imwrite(path, tile)
79
 
80
+ frame_index += 1
 
81
 
82
 
83
  @app.get("/youtube")
84
+ def youtube(url: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
+ thread = threading.Thread(
87
+ target=stream_youtube,
88
+ args=(url,)
89
+ )
90
 
91
+ thread.start()
92
 
93
+ return {"status": "stream started"}
 
94
 
 
95
 
96
+ @app.get("/frame/{frame}/{map}")
97
+ def get_frame(frame: int, map: int):
98
 
99
+ path = f"{FRAME_FOLDER}/frame_{frame}_map{map}.png"
100
 
101
+ if os.path.exists(path):
102
+ return {"url": f"/frames/frame_{frame}_map{map}.png"}
103
 
104
+ return {"error": "not ready"}
105
  if __name__ == "__main__":
 
106
  uvicorn.run(app, host="0.0.0.0", port=7860)