generated from pavel.muhortov/template-bash
	add FFmpeg.probe()
This commit is contained in:
		
							parent
							
								
									c700997ed3
								
							
						
					
					
						commit
						3e938f20cc
					
				|  | @ -1,6 +1,7 @@ | ||||||
| #!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | import json | ||||||
| import logging | import logging | ||||||
| import urllib.request | import urllib.request | ||||||
| from argparse import ArgumentParser | from argparse import ArgumentParser | ||||||
|  | @ -1204,7 +1205,7 @@ class Proc: | ||||||
|         return execlist |         return execlist | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|     def list(cls) -> list: |     def list_all(cls) -> list: | ||||||
|         """Find all running process. |         """Find all running process. | ||||||
| 
 | 
 | ||||||
|         Returns: |         Returns: | ||||||
|  | @ -1230,7 +1231,7 @@ class Proc: | ||||||
|         """ |         """ | ||||||
|         proc_found = [] |         proc_found = [] | ||||||
|         try: |         try: | ||||||
|             for proc in cls.list(): |             for proc in cls.list_all(): | ||||||
|                 if exclude and ( |                 if exclude and ( | ||||||
|                     exclude in proc['execpid'] or |                     exclude in proc['execpid'] or | ||||||
|                     exclude in proc['exename'] or |                     exclude in proc['exename'] or | ||||||
|  | @ -1272,34 +1273,43 @@ class FFmpeg: | ||||||
|     @classmethod |     @classmethod | ||||||
|     def run( |     def run( | ||||||
|         cls, |         cls, | ||||||
|         src: str, |         src: (str, type(None)) = None, | ||||||
|         dst: str = None, |         dst: str = None, | ||||||
|         fps: int = None, |         fps: int = None, | ||||||
|         preset: str = None, |         preset: str = None, | ||||||
|  |         raw: (str, type(None)) = None, | ||||||
|         ffpath: str = None, |         ffpath: str = None, | ||||||
|         watchdog: bool = False, |         watchdog: bool = False, | ||||||
|         watchsec: int = None, |         watchsec: int = None, | ||||||
|         onlyonce: bool = False |         onlyonce: bool = False | ||||||
|     ) -> None: |         ) -> int: | ||||||
|         """Running the installed ffmpeg |         """Running the installed ffmpeg. | ||||||
| 
 | 
 | ||||||
|         Args: |         Args: | ||||||
|             src (str): sources urls (example: "rtsp://user:pass@host:554/Streaming/Channels/101, anull"). |             src (str, type, optional): sources urls, example: | ||||||
|             dst (str, optional): destination url (example: rtp://239.0.0.1:5554). Defaults to None. |                 'rtsp://user:pass@host:554/Streaming/Channels/101, anull'. Defaults to None. | ||||||
|  |             dst (str, optional): destination url, example: 'rtp://239.0.0.1:5554'. Defaults to None. | ||||||
|             fps (int, optional): frame per second encoding output. Defaults to None. |             fps (int, optional): frame per second encoding output. Defaults to None. | ||||||
|             preset (str, optional): 240p, 360p, 480p, 720p, 1080p, 1440p, 2160p. Defaults to None. |             preset (str, optional): 240p, 360p, 480p, 720p, 1080p, 1440p, 2160p. Defaults to None. | ||||||
|             ffpath (str, optional): alternative path to bin (example: /usr/bin/ffmpeg). Defaults to None. |             raw (str, type, optional): custom ffmpeg parameters string. Defaults to None. | ||||||
|  |             ffpath (str, optional): custom path to bin, example: /usr/bin/ffmpeg. Defaults to None. | ||||||
|             watchdog (bool, optional): detect ffmpeg freeze and terminate. Defaults to False. |             watchdog (bool, optional): detect ffmpeg freeze and terminate. Defaults to False. | ||||||
|             watchsec (int, optional): seconds to wait before the watchdog terminates. Defaults to None. |             watchsec (int, optional): seconds to wait before watchdog terminates. Defaults to None. | ||||||
|             onlyonce (bool, optional): detect ffmpeg running copy and terminate. Defaults to False. |             onlyonce (bool, optional): detect ffmpeg running copy and terminate. Defaults to False. | ||||||
|         """ |  | ||||||
| 
 | 
 | ||||||
|         process = ( |         Returns: | ||||||
|             cls._bin(ffpath).split() + |             int: ffmpeg return code | ||||||
|             cls._src(src).split() + |         """ | ||||||
|             cls._preset(preset, fps).split() + |         if not raw: | ||||||
|             cls._dst(dst).split() |             process = ('' | ||||||
|  |                 + cls._bin(ffpath).split() | ||||||
|  |                 + cls._src(src).split() | ||||||
|  |                 + cls._preset(preset, fps).split() | ||||||
|  |                 + cls._dst(dst).split() | ||||||
|             ) |             ) | ||||||
|  |         else: | ||||||
|  |             process = cls._bin(ffpath).split() + raw.split() | ||||||
|  | 
 | ||||||
|         if onlyonce and Proc.search(' '.join(process)): |         if onlyonce and Proc.search(' '.join(process)): | ||||||
|             print('Process already exist, exit...') |             print('Process already exist, exit...') | ||||||
|         else: |         else: | ||||||
|  | @ -1318,17 +1328,18 @@ class FFmpeg: | ||||||
|                         print(line, flush=True) |                         print(line, flush=True) | ||||||
|                     else: |                     else: | ||||||
|                         que.put(line) |                         que.put(line) | ||||||
|         exit() |         return proc.returncode | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|     def _bin(cls, path_ffmpeg: str) -> str: |     def _bin(cls, ffpath: str, tool: str = 'ffmpeg') -> str: | ||||||
|         """Returns the path to the ffmpeg depending on the OS. |         """Returns the path to the bin depending on the OS. | ||||||
| 
 | 
 | ||||||
|         Args: |         Args: | ||||||
|             path_ffmpeg (str): alternative path to bin. |             ffpath (str): custom path to bin. | ||||||
|  |             tool (str, optional): 'ffmpeg', 'ffprobe'. Defaults to 'ffmpeg'. | ||||||
| 
 | 
 | ||||||
|         Returns: |         Returns: | ||||||
|             str: path to ffmpeg. |             str: path to bin or None, if path does not exist. | ||||||
|         """ |         """ | ||||||
|         faq = ( |         faq = ( | ||||||
|             '\n' |             '\n' | ||||||
|  | @ -1346,15 +1357,21 @@ class FFmpeg: | ||||||
|             '\tDownload and extract archive from: https://evermeet.cx/ffmpeg/\n' |             '\tDownload and extract archive from: https://evermeet.cx/ffmpeg/\n' | ||||||
|             '\tTarget: /usr/bin/ffmpeg\n' |             '\tTarget: /usr/bin/ffmpeg\n' | ||||||
|             ) |             ) | ||||||
|         if not path_ffmpeg: |         if not ffpath: | ||||||
|             if platform.startswith('linux') or platform.startswith('darwin'): |             if platform.startswith('linux') or platform.startswith('darwin'): | ||||||
|                 path_ffmpeg = '/usr/bin/ffmpeg' |                 if tool == 'ffprobe': | ||||||
|             elif platform.startswith('win32'): |                     ffpath = '/usr/bin/ffprobe' | ||||||
|                 path_ffmpeg = environ['PROGRAMFILES'] + "\\ffmpeg\\bin\\ffmpeg.exe" |  | ||||||
|         if path.exists(path_ffmpeg): |  | ||||||
|             return path_ffmpeg |  | ||||||
|                 else: |                 else: | ||||||
|             print('ON', platform, 'PLATFORM', 'not found ffmpeg', faq) |                     ffpath = '/usr/bin/ffmpeg' | ||||||
|  |             elif platform.startswith('win32'): | ||||||
|  |                 if tool == 'ffprobe': | ||||||
|  |                     ffpath = environ['PROGRAMFILES'] + "\\ffmpeg\\bin\\ffprobe.exe" | ||||||
|  |                 else: | ||||||
|  |                     ffpath = environ['PROGRAMFILES'] + "\\ffmpeg\\bin\\ffmpeg.exe" | ||||||
|  |         if path.exists(ffpath): | ||||||
|  |             return ffpath | ||||||
|  |         else: | ||||||
|  |             print('ON', platform, 'PLATFORM', 'not found', tool, faq) | ||||||
|             return None |             return None | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|  | @ -1471,6 +1488,41 @@ class FFmpeg: | ||||||
|                     break |                     break | ||||||
|             exit() |             exit() | ||||||
| 
 | 
 | ||||||
|  |     @classmethod | ||||||
|  |     def probe( | ||||||
|  |         cls, | ||||||
|  |         target: (str, type(None)) = None, | ||||||
|  |         raw: (str, type(None)) = None, | ||||||
|  |         ffpath: str = None | ||||||
|  |         ) -> (dict, bytes, None): | ||||||
|  |         """Running the installed ffprobe. | ||||||
|  | 
 | ||||||
|  |         Args: | ||||||
|  |             target (str, type, optional): media file path to probe. Defaults to None. | ||||||
|  |             raw (str, type, optional): custom ffprobe parameters string. Defaults to None. | ||||||
|  |             ffpath (str, optional): custom path to bin, example: /usr/bin/ffprobe. Defaults to None. | ||||||
|  | 
 | ||||||
|  |         Returns: | ||||||
|  |             dict, bytes, None: ffprobe response or None. | ||||||
|  |         """ | ||||||
|  |         if not raw: | ||||||
|  |             command = ([] | ||||||
|  |                 + cls._bin(ffpath=ffpath, tool='ffprobe').split() | ||||||
|  |                 + ('-i ' + target | ||||||
|  |                 + ' -v quiet -print_format json -show_format -show_programs -show_streams').split() | ||||||
|  |                 ) | ||||||
|  |         else: | ||||||
|  |             command = cls._bin(ffpath=ffpath, tool='ffprobe').split() + raw.split() | ||||||
|  | 
 | ||||||
|  |         with Popen(command, stdout=PIPE, stderr=STDOUT) as process: | ||||||
|  |             result = process.communicate() | ||||||
|  |             if process.returncode == 0 and not raw: | ||||||
|  |                 return json.loads(result[0].decode('utf-8')) | ||||||
|  |             elif process.returncode == 0 and raw: | ||||||
|  |                 return result[0] | ||||||
|  |             else: | ||||||
|  |                 return None | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     time_start = datetime.now() |     time_start = datetime.now() | ||||||
|  | @ -1556,7 +1608,7 @@ if __name__ == "__main__": | ||||||
|                     watchsec = None |                     watchsec = None | ||||||
|                     if 'watchsec' in broadcast_config: |                     if 'watchsec' in broadcast_config: | ||||||
|                         watchsec = int(broadcast_config['watchsec']) |                         watchsec = int(broadcast_config['watchsec']) | ||||||
|                     monopoly = None |                     onlyonce = None | ||||||
|                     if 'onlyonce' in broadcast_config: |                     if 'onlyonce' in broadcast_config: | ||||||
|                         onlyonce = broadcast_config['onlyonce'] |                         onlyonce = broadcast_config['onlyonce'] | ||||||
|                     FFmpeg.run( |                     FFmpeg.run( | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user