Move ffmpeg_cut_segment to new ffmpeg_cut_many() system

pull/400/head
Mike Lang 6 months ago committed by Mike Lang
parent ba36338db4
commit cc789caa7e

@ -11,7 +11,7 @@ import logging
import os import os
import shutil import shutil
from collections import namedtuple from collections import namedtuple
from contextlib import closing, contextmanager from contextlib import contextmanager
from tempfile import TemporaryFile from tempfile import TemporaryFile
from uuid import uuid4 from uuid import uuid4
@ -358,14 +358,12 @@ def streams_info(segment):
def ffmpeg_cut_segment(segment, cut_start=None, cut_end=None): def ffmpeg_cut_segment(segment, cut_start=None, cut_end=None):
"""Return a Popen object which is ffmpeg cutting the given single segment.
This is used when doing a fast cut.
""" """
args = [ Wrapper for ffmpeg_cut_many() which cuts a single segment file to stdout,
'ffmpeg', taking care to preserve stream order and other metadata.
'-hide_banner', '-loglevel', 'error', # suppress noisy output Used when doing a fast cut.
'-i', segment.path, """
] args = []
# output from ffprobe is generally already sorted but let's be paranoid, # output from ffprobe is generally already sorted but let's be paranoid,
# because the order of map args matters. # because the order of map args matters.
for stream in sorted(streams_info(segment), key=lambda stream: stream['index']): for stream in sorted(streams_info(segment), key=lambda stream: stream['index']):
@ -384,11 +382,10 @@ def ffmpeg_cut_segment(segment, cut_start=None, cut_end=None):
# as it changes the order that frames go in the file, which messes with our "concatenate the # as it changes the order that frames go in the file, which messes with our "concatenate the
# packets" method of concatenating the video. # packets" method of concatenating the video.
args += ['-bf', '0'] args += ['-bf', '0']
# output to stdout as MPEG-TS # output as MPEG-TS
args += ['-f', 'mpegts', '-'] args += ['-f', 'mpegts']
# run it
logging.info("Running segment cut with args: {}".format(" ".join(args))) return ffmpeg_cut_one([segment], args)
return subprocess.Popen(args, stdout=subprocess.PIPE)
def ffmpeg_cut_one(segments, encode_args, output_file=subprocess.PIPE, input_args=[]): def ffmpeg_cut_one(segments, encode_args, output_file=subprocess.PIPE, input_args=[]):
@ -620,31 +617,13 @@ def fast_cut_range(segments, start, end, fixts=None):
if segment in (first, last): if segment in (first, last):
if fixts: if fixts:
fixts.next() fixts.next()
proc = None with ffmpeg_cut_segment(
try:
proc = ffmpeg_cut_segment(
segment, segment,
cut_start if segment == first else None, cut_start if segment == first else None,
cut_end if segment == last else None, cut_end if segment == last else None,
) ) as ffmpeg:
with closing(proc.stdout): for chunk in read_chunks(ffmpeg.stdout):
for chunk in read_chunks(proc.stdout):
yield fixts.feed(chunk) if fixts else chunk yield fixts.feed(chunk) if fixts else chunk
proc.wait()
except Exception as ex:
# try to clean up proc, ignoring errors
if proc is not None:
try:
proc.kill()
except OSError:
pass
raise ex
else:
# check if ffmpeg had errors
if proc.returncode != 0:
raise Exception(
"Error while streaming cut: ffmpeg exited {}".format(proc.returncode)
)
if fixts: if fixts:
fixts.next() fixts.next()
else: else:

Loading…
Cancel
Save