Upload 4 files
Browse files- .gitattributes +1 -0
- Dockerfile +11 -0
- main.py +69 -0
- news_video.mp4 +3 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
news_video.mp4 filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import subprocess
|
| 3 |
+
import threading
|
| 4 |
+
from fastapi import FastAPI, WebSocket
|
| 5 |
+
import nest_asyncio
|
| 6 |
+
# from pyngrok import ngrok
|
| 7 |
+
import uvicorn
|
| 8 |
+
from asyncio import sleep
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class StreamProcess:
|
| 14 |
+
def __init__(self, video_path, stream_key):
|
| 15 |
+
self.video_path = video_path
|
| 16 |
+
self.stream_key = stream_key
|
| 17 |
+
self.process = None
|
| 18 |
+
self.stop_event = threading.Event()
|
| 19 |
+
|
| 20 |
+
def start(self):
|
| 21 |
+
args = [
|
| 22 |
+
'-stream_loop', '-1',
|
| 23 |
+
'-re',
|
| 24 |
+
'-i', self.video_path,
|
| 25 |
+
'-c', 'copy',
|
| 26 |
+
'-f', 'flv',
|
| 27 |
+
'-fflags', 'nobuffer',
|
| 28 |
+
'-flags', 'low_delay',
|
| 29 |
+
f'rtmp://a.rtmp.youtube.com/live2/{self.stream_key}'
|
| 30 |
+
]
|
| 31 |
+
self.process = subprocess.Popen(['ffmpeg'] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 32 |
+
def check_stream(self):
|
| 33 |
+
if self.process and self.process.poll() is None:
|
| 34 |
+
# print("Streaming process running")
|
| 35 |
+
return True
|
| 36 |
+
# print("Streaming process stopped")
|
| 37 |
+
return False
|
| 38 |
+
def stop(self):
|
| 39 |
+
if self.process and self.process.poll() is None:
|
| 40 |
+
self.stop_event.set() # Signal the thread to stop
|
| 41 |
+
self.process.terminate() # Terminate the ffmpeg process
|
| 42 |
+
|
| 43 |
+
def stream_video(video_path, stream_key):
|
| 44 |
+
stream_process = StreamProcess(video_path, stream_key)
|
| 45 |
+
stream_thread = threading.Thread(target=stream_process.start)
|
| 46 |
+
stream_thread.start()
|
| 47 |
+
return stream_process
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
stream_process = None
|
| 52 |
+
|
| 53 |
+
@app.get('/startStreaming')
|
| 54 |
+
async def startStreaming():
|
| 55 |
+
global stream_process
|
| 56 |
+
video_path='./news_video.mp4'
|
| 57 |
+
stream_key='68p2-txmk-79sm-hjfh-2d26'
|
| 58 |
+
stream_process = stream_video(video_path,stream_key)
|
| 59 |
+
return {"stream_status":stream_process.check_stream()}
|
| 60 |
+
|
| 61 |
+
@app.get('/stopStreaming')
|
| 62 |
+
async def stopStreaming():
|
| 63 |
+
global stream_process
|
| 64 |
+
if stream_process:
|
| 65 |
+
stream_process.stop()
|
| 66 |
+
return {"stream_status":stream_process.check_stream()}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
news_video.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ae8e0b19edcd67460be3ebd6059311567888f7e41bea6f2176e12bfa9994331d
|
| 3 |
+
size 22202860
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
pyngrok
|
| 3 |
+
uvicorn
|
| 4 |
+
ffmpeg
|
| 5 |
+
nest-asyncio
|
| 6 |
+
python-dotenv
|