mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-10-04 12:34:53 -04:00
[utils] Popen
: Refactor to use contextmanager
Fixes https://github.com/yt-dlp/yt-dlp/issues/3531#issuecomment-1156223597
This commit is contained in:
@@ -157,14 +157,12 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
|
||||
|
||||
self._report_run('atomicparsley', filename)
|
||||
self.write_debug('AtomicParsley command line: %s' % shell_quote(cmd))
|
||||
p = Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate_or_kill()
|
||||
if p.returncode != 0:
|
||||
msg = stderr.decode('utf-8', 'replace').strip()
|
||||
self.report_warning(f'Unable to embed thumbnails using AtomicParsley; {msg}')
|
||||
stdout, stderr, returncode = Popen.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
if returncode:
|
||||
self.report_warning(f'Unable to embed thumbnails using AtomicParsley; {stderr.strip()}')
|
||||
# for formats that don't support thumbnails (like 3gp) AtomicParsley
|
||||
# won't create to the temporary file
|
||||
if b'No changes' in stdout:
|
||||
if 'No changes' in stdout:
|
||||
self.report_warning('The file format doesn\'t support embedding a thumbnail')
|
||||
success = False
|
||||
|
||||
|
@@ -239,14 +239,12 @@ class FFmpegPostProcessor(PostProcessor):
|
||||
encodeArgument('-i')]
|
||||
cmd.append(encodeFilename(self._ffmpeg_filename_argument(path), True))
|
||||
self.write_debug(f'{self.basename} command line: {shell_quote(cmd)}')
|
||||
handle = Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout_data, stderr_data = handle.communicate_or_kill()
|
||||
expected_ret = 0 if self.probe_available else 1
|
||||
if handle.wait() != expected_ret:
|
||||
stdout, stderr, returncode = Popen.run(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
if returncode != (0 if self.probe_available else 1):
|
||||
return None
|
||||
except OSError:
|
||||
return None
|
||||
output = (stdout_data if self.probe_available else stderr_data).decode('ascii', 'ignore')
|
||||
output = stdout if self.probe_available else stderr
|
||||
if self.probe_available:
|
||||
audio_codec = None
|
||||
for line in output.split('\n'):
|
||||
@@ -280,11 +278,10 @@ class FFmpegPostProcessor(PostProcessor):
|
||||
]
|
||||
|
||||
cmd += opts
|
||||
cmd.append(encodeFilename(self._ffmpeg_filename_argument(path), True))
|
||||
self.write_debug('ffprobe command line: %s' % shell_quote(cmd))
|
||||
p = Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate()
|
||||
return json.loads(stdout.decode('utf-8', 'replace'))
|
||||
cmd.append(self._ffmpeg_filename_argument(path))
|
||||
self.write_debug(f'ffprobe command line: {shell_quote(cmd)}')
|
||||
stdout, _, _ = Popen.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
return json.loads(stdout)
|
||||
|
||||
def get_stream_number(self, path, keys, value):
|
||||
streams = self.get_metadata_object(path)['streams']
|
||||
@@ -346,16 +343,13 @@ class FFmpegPostProcessor(PostProcessor):
|
||||
for i, (path, opts) in enumerate(path_opts) if path)
|
||||
|
||||
self.write_debug('ffmpeg command line: %s' % shell_quote(cmd))
|
||||
p = Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate_or_kill()
|
||||
if p.returncode not in variadic(expected_retcodes):
|
||||
stderr = stderr.decode('utf-8', 'replace').strip()
|
||||
self.write_debug(stderr)
|
||||
raise FFmpegPostProcessorError(stderr.split('\n')[-1])
|
||||
stdout, stderr, returncode = Popen.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
if returncode not in variadic(expected_retcodes):
|
||||
raise FFmpegPostProcessorError(stderr.strip().splitlines()[-1])
|
||||
for out_path, _ in output_path_opts:
|
||||
if out_path:
|
||||
self.try_utime(out_path, oldest_mtime, oldest_mtime)
|
||||
return stderr.decode('utf-8', 'replace')
|
||||
return stderr
|
||||
|
||||
def run_ffmpeg(self, path, out_path, opts, **kwargs):
|
||||
return self.run_ffmpeg_multiple_files([path], out_path, opts, **kwargs)
|
||||
|
@@ -84,17 +84,15 @@ class SponSkrubPP(PostProcessor):
|
||||
cmd = [encodeArgument(i) for i in cmd]
|
||||
|
||||
self.write_debug('sponskrub command line: %s' % shell_quote(cmd))
|
||||
pipe = None if self.get_param('verbose') else subprocess.PIPE
|
||||
p = Popen(cmd, stdout=pipe)
|
||||
stdout = p.communicate_or_kill()[0]
|
||||
stdout, _, returncode = Popen.run(cmd, text=True, stdout=None if self.get_param('verbose') else subprocess.PIPE)
|
||||
|
||||
if p.returncode == 0:
|
||||
if not returncode:
|
||||
os.replace(temp_filename, filename)
|
||||
self.to_screen('Sponsor sections have been %s' % ('removed' if self.cutout else 'marked'))
|
||||
elif p.returncode == 3:
|
||||
elif returncode == 3:
|
||||
self.to_screen('No segments in the SponsorBlock database')
|
||||
else:
|
||||
msg = stdout.decode('utf-8', 'replace').strip() if stdout else ''
|
||||
msg = msg.split('\n')[0 if msg.lower().startswith('unrecognised') else -1]
|
||||
raise PostProcessingError(msg if msg else 'sponskrub failed with error code %s' % p.returncode)
|
||||
raise PostProcessingError(
|
||||
stdout.strip().splitlines()[0 if stdout.strip().lower().startswith('unrecognised') else -1]
|
||||
or f'sponskrub failed with error code {returncode}')
|
||||
return [], information
|
||||
|
Reference in New Issue
Block a user