-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move more ffmpeg-specific functions to ffmpeg.py
- Loading branch information
Showing
7 changed files
with
81 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[flake8] | ||
max-line-length = 132 | ||
exclude = .git,__pycache__,.eggs/,doc/,docs/,build/,dist/,archive/ | ||
per-file-ignores = | ||
__init__.py:F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,22 @@ | |
import os | ||
from pathlib import Path | ||
import shutil | ||
import json | ||
|
||
|
||
class Ffmpeg(): | ||
|
||
class Ffmpeg: | ||
def __init__(self): | ||
|
||
self.ERROR = ['-loglevel', 'error'] | ||
self.WARNING = ['-loglevel', 'warning'] | ||
self.INFO = ['-loglevel', 'info'] | ||
self.ERROR = ["-loglevel", "error"] | ||
self.WARNING = ["-loglevel", "warning"] | ||
self.INFO = ["-loglevel", "info"] | ||
|
||
self.YES = ['-y'] | ||
self.YES = ["-y"] | ||
|
||
# default 8, increasing can help avoid warnings | ||
self.QUEUE = ['-thread_queue_size', '8'] | ||
self.QUEUE = ["-thread_queue_size", "8"] | ||
|
||
self.THROTTLE = '-re' | ||
self.THROTTLE = "-re" | ||
|
||
def timelimit(self, t: Union[str, int, float]) -> List[str]: | ||
if t is None: | ||
|
@@ -30,7 +30,7 @@ def timelimit(self, t: Union[str, int, float]) -> List[str]: | |
t = str(t) | ||
|
||
if len(t) > 0: | ||
return ['-t', str(t)] | ||
return ["-t", str(t)] | ||
else: | ||
return [] | ||
|
||
|
@@ -39,16 +39,15 @@ def drawtext(self, text: str = None) -> List[str]: | |
if not text: # None or '' or [] etc. | ||
return [] | ||
|
||
fontcolor = 'fontcolor=white' | ||
fontsize = 'fontsize=24' | ||
box = 'box=1' | ||
boxcolor = '[email protected]' | ||
border = 'boxborderw=5' | ||
x = 'x=(w-text_w)/2' | ||
y = 'y=(h-text_h)*3/4' | ||
fontcolor = "fontcolor=white" | ||
fontsize = "fontsize=24" | ||
box = "box=1" | ||
boxcolor = "[email protected]" | ||
border = "boxborderw=5" | ||
x = "x=(w-text_w)/2" | ||
y = "y=(h-text_h)*3/4" | ||
|
||
return ['-vf', | ||
f"drawtext=text='{text}':{fontcolor}:{fontsize}:{box}:{boxcolor}:{border}:{x}:{y}"] | ||
return ["-vf", f"drawtext=text='{text}':{fontcolor}:{fontsize}:{box}:{boxcolor}:{border}:{x}:{y}"] | ||
|
||
def listener(self): | ||
""" | ||
|
@@ -63,19 +62,19 @@ def listener(self): | |
|
||
TIMEOUT = 0.5 | ||
|
||
FFPLAY = shutil.which('ffplay') | ||
FFPLAY = shutil.which("ffplay") | ||
if not FFPLAY: | ||
raise FileNotFoundError('FFplay not found, cannot start listener') | ||
raise FileNotFoundError("FFplay not found, cannot start listener") | ||
|
||
cmd = [FFPLAY, '-loglevel', 'error', '-timeout', '5', '-autoexit', 'rtmp://localhost'] | ||
cmd = [FFPLAY, "-loglevel", "error", "-timeout", "5", "-autoexit", "rtmp://localhost"] | ||
|
||
print('starting Localhost RTMP listener. \n\n', ' '.join(cmd), '\n\n Press q in this terminal to end stream.') | ||
print("starting Localhost RTMP listener. \n\n", " ".join(cmd), "\n\n Press q in this terminal to end stream.") | ||
|
||
proc = subprocess.Popen(cmd) | ||
|
||
# proc = subprocess.Popen(['ffmpeg', '-v', 'fatal', '-timeout', '5', | ||
# '-i', 'rtmp://localhost', '-f', 'null', '-'], | ||
# stdout=subprocess.DEVNULL) | ||
# proc = subprocess.Popen(['ffmpeg', '-v', 'fatal', '-timeout', '5', | ||
# '-i', 'rtmp://localhost', '-f', 'null', '-'], | ||
# stdout=subprocess.DEVNULL) | ||
|
||
sleep(TIMEOUT) | ||
|
||
|
@@ -86,7 +85,44 @@ def movingBG(self, bgfn: Path = None) -> List[str]: | |
return [] | ||
|
||
bg = str(bgfn) | ||
if os.name == 'nt': | ||
bg = bg.replace('\\', '/') # for PureWindowsPath | ||
if os.name == "nt": | ||
bg = bg.replace("\\", "/") # for PureWindowsPath | ||
|
||
return ["-filter_complex", f"movie={bg}:loop=0,setpts=N/FRAME_RATE/TB"] | ||
|
||
|
||
def get_exe(exein: str) -> str: | ||
"""checks that host streaming program is installed""" | ||
|
||
exe = str(Path(exein).expanduser()) | ||
# %% verify | ||
if not shutil.which(exe): | ||
raise FileNotFoundError( | ||
f""" | ||
*** Must have FFmpeg + FFprobe installed to use PyLivestream. | ||
https://www.ffmpeg.org/download.html | ||
could not find {exein} | ||
""" | ||
) | ||
|
||
return exe | ||
|
||
|
||
def get_meta(fn: Path, exein: str = None) -> Union[None, dict]: | ||
if not fn: # audio-only | ||
return None | ||
|
||
fn = Path(fn).expanduser() | ||
|
||
if not fn.is_file(): | ||
raise FileNotFoundError(fn) | ||
|
||
exe = get_exe("ffprobe") if exein is None else exein | ||
|
||
cmd = [str(exe), "-loglevel", "error", "-print_format", "json", "-show_streams", "-show_format", str(fn)] | ||
|
||
return ['-filter_complex', f'movie={bg}:loop=0,setpts=N/FRAME_RATE/TB'] | ||
ret = subprocess.check_output(cmd, universal_newlines=True) | ||
# %% decode JSON from FFprobe | ||
return json.loads(ret) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters