Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 56 additions & 11 deletions bilix/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,29 @@


async def concat(path_lst: List[Path], output_path: Path, remove=True):
with tempfile.NamedTemporaryFile('w', dir=output_path.parent, delete=False) as fp:
with tempfile.NamedTemporaryFile(
'w', dir=output_path.parent, delete_on_close=False
) as fp:
for path in path_lst:
fp.write(f"file '{path.name}'\n")
cmd = ['ffmpeg', '-f', 'concat', '-safe', '0', '-i', fp.name, '-c', 'copy', '-loglevel', 'quiet',
str(output_path)]
fp.close()
cmd = [
'ffmpeg',
'-f',
'concat',
'-safe',
'0',
'-i',
fp.name,
'-c',
'copy',
'-loglevel',
'quiet',
'-bitexact',
str(output_path),
]
# print(' '.join(map(lambda x: f'"{x}"', cmd)))
await run_process(cmd)
os.remove(fp.name)
await run_process(cmd)
if remove:
for path in path_lst:
os.remove(path)
Expand All @@ -26,19 +41,49 @@ async def combine(path_lst: List[Path], output_path: Path, remove=True):
cmd = ['ffmpeg']
for path in path_lst:
cmd.extend(['-i', str(path)])
# for flac, use -strict -2
cmd.extend(['-c', 'copy', '-strict', '-2', '-loglevel', 'quiet', str(output_path)])
# for flac, use -strict experimental
cmd.extend(
[
'-c',
'copy',
'-strict',
'experimental',
'-loglevel',
'quiet',
'-bitexact',
str(output_path),
]
)
# print(' '.join(map(lambda x: f'"{x}"', cmd)))
await run_process(cmd)
if remove:
for path in path_lst:
os.remove(path)


async def time_range_clip(input_path: Path, start: int, t: int, output_path: Path, remove=True):
# for flac, use -strict -2
cmd = ['ffmpeg', '-ss', f'{start:.1f}', '-t', f'{t:.1f}', '-i', str(input_path), '-codec', 'copy', '-strict', '-2',
'-loglevel', 'quiet', '-f', 'mp4', str(output_path)]
async def time_range_clip(
input_path: Path, start: int, t: int, output_path: Path, remove=True
):
# for flac, use -strict experimental
cmd = [
'ffmpeg',
'-ss',
f'{start:.1f}',
'-t',
f'{t:.1f}',
'-i',
str(input_path),
'-codec',
'copy',
'-strict',
'experimental',
'-loglevel',
'quiet',
'-f',
'mp4',
'-bitexact',
str(output_path),
]
# print(' '.join(map(lambda x: f'"{x}"', cmd)))
await run_process(cmd)
if remove:
Expand Down