Skip to content

Instantly share code, notes, and snippets.

@suminb
Created December 30, 2020 15:10
Show Gist options
  • Save suminb/308f342ab74467009cacc59ad9466288 to your computer and use it in GitHub Desktop.
Save suminb/308f342ab74467009cacc59ad9466288 to your computer and use it in GitHub Desktop.
Concat GoPro media files
import glob
import os
import sys
def escape(path):
return path.replace(" ", "\ ")
def extract_episode_and_sequence(path):
_, tail = os.path.split(path)
seq, ep = tail[:4], tail[4:8]
return ep, seq
def group_by_episode(pairs):
group = {}
for ep, seq in pairs:
group.setdefault(ep, [])
group[ep].append(seq)
return group
if __name__ == "__main__":
path = sys.argv[1]
if os.path.isdir(path):
dirname = path
else:
dirname = os.path.dirname(path)
paths = glob.glob(f"{dirname}/*.MP4")
pairs = [extract_episode_and_sequence(p) for p in paths]
group = group_by_episode(pairs)
for episode in group:
sequences = group[episode]
if len(sequences) > 1:
prefix = sequences[0][:2]
command = f"ffmpeg -f concat -safe -0 -i <(for f in {escape(dirname)}/{prefix}??{episode}.MP4; do echo \"file '$f'\"; done) -c copy {escape(dirname)}/{prefix}00{episode}.mp4"
print(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment