Skip to content

Instantly share code, notes, and snippets.

@search5
Created January 17, 2022 04:33
Show Gist options
  • Save search5/9b39d08ddefea0d5ad77f9efca8c162e to your computer and use it in GitHub Desktop.
Save search5/9b39d08ddefea0d5ad77f9efca8c162e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import subprocess
import shlex
import os, sys
from datetime import timedelta, datetime
parser = argparse.ArgumentParser(description='통째 영상 파일을 여러개의 비디오 파일로 분할합니다')
parser.add_argument('description', metavar='description', type=str, nargs=1, help='분할 영상 정보 파일을 제공하세요')
args = parser.parse_args()
description = filter(lambda x: bool(x), open(args.description[0]).readlines())
# 동영상 파일 자동 가져오기
current_filename, ext = os.path.splitext(args.description[0])
movie_file = tuple(filter(lambda item: (item != __file__) and item.startswith(current_filename) and not item.endswith(".txt"), os.listdir()))[0]
song_information = []
for item in description:
start, filename = item.split(' ', 1)
song_information.append(dict(
start=start,
end=0,
filename=filename.strip() + ".mp3"
))
if len(song_information) > 0:
start_time_split = start.split(":")
if start.count(":") == 1:
start_timedelta = timedelta(minutes=int(start_time_split[0]), seconds=int(start_time_split[1]))
else:
start_timedelta = timedelta(hours=int(start_time_split[0]), minutes=int(start_time_split[1]), seconds=int(start_time_split[2]))
try:
end_time = datetime.fromtimestamp((start_timedelta - timedelta(seconds=1)).total_seconds())
if start.count(":") == 1:
end_time = end_time.replace(hour=0)
song_information[-2]['end'] = end_time.strftime('%H:%M:%S')
except:
pass
ffmpeg_info = subprocess.run(shlex.split(f"ffmpeg -i \"{movie_file}\""), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
duration = tuple(filter(lambda line: 'Duration' in line, ffmpeg_info.stderr.decode('utf8').splitlines()))[0].strip()
duration = duration[:duration.find(",")].split(":", 1)[-1].strip()
song_information[-1]['end'] = duration[:duration.find(".")]
# 동영상 쪼개기..
for item in song_information:
subprocess.run(shlex.split(f"ffmpeg -i \"{movie_file}\" -ss {item['start']} -to {item['end']} -vn -c:a libmp3lame -ac 2 -b:a 128k -ar 48000 \"{item['filename']}\""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment