Browse Source

First commit

lzl12051 3 years ago
parent
commit
84b68135fa
2 changed files with 78 additions and 0 deletions
  1. 11 0
      README.md
  2. 67 0
      concatGOPRO.py

+ 11 - 0
README.md

@@ -1,2 +1,13 @@
 # ConcatGoproVideos
 A python script that can help you to concatenate GoPro's 4GB chaptered videos.
+
+The script will scan the file names ([GoPro File naming Convention](https://community.gopro.com/s/article/GoPro-Camera-File-Naming-Convention?language=en_US), e.g. GH011234.mp4) in your input directory (maybe GoPro's TF card) and find out all chaptered videos. And it will generate a file for FFmpeg to concatenate all videos.
+
+# Usage
+**FFmpeg must be installed first to use this script.**
+
+```
+python3 concatGOPRO.py input_path output_path
+```
+
+And go check your output folder!

+ 67 - 0
concatGOPRO.py

@@ -0,0 +1,67 @@
+import os
+import sys
+import shutil
+
+def getVideoList(dir: str) -> dict[list[list, str]]:
+    all_videos = dict()
+    video_formats = set(["mp4", "MP4"])
+    all_files = os.listdir(dir)
+    for file_name in all_files:
+        try:
+            isG = file_name[0]
+            code_type = file_name[1]
+            ext_name = file_name[-3:]
+            video_chapter = file_name[2:4]
+            video_idx = file_name[4:8]
+        except IndexError:
+            continue
+        # GoPro files start with "G"
+        if isG == 'G' and ext_name in video_formats:
+            try:
+                all_videos[video_idx][0].append(file_name)
+            except KeyError:
+                all_videos[video_idx] = [[file_name], code_type]
+    return all_videos
+
+
+def pathValidation(source_dir, output_dir):
+    if not os.path.isdir(source_dir):
+        return False
+    if not os.path.isdir(output_dir):
+        try:
+            os.mkdir(output_dir)
+        except:
+            raise OSError("Faild to create the output folder.")
+    return True
+
+
+if __name__ == "__main__":
+    _, source_dir, output_dir = sys.argv
+    # print(_, source_dir, output_dir)
+    if pathValidation(source_dir, output_dir):
+        videos = getVideoList(source_dir)
+        chaptered_video_num = 0
+        # print(file_list)
+        for name in videos.keys():
+            if len(videos[name][0]) > 1:
+                chaptered_video_num += 1
+                os.chdir(output_dir)
+                try:
+                    os.mkdir("temp")
+                except FileExistsError:
+                    pass
+                os.chdir("temp")
+                with open(f"{name}.join", 'w') as file:
+                    chapters = videos[name][0]
+                    for c in chapters:
+                        file.write(f"file '{source_dir}/{c}'\n")
+                status = os.system(f"ffmpeg -f concat -safe 0 -i {name}.join -c copy ../G{name}_concat.mp4")
+                if status:
+                    raise OSError("Something Goes Wrong With FFmpeg.")
+                    break
+        os.chdir(output_dir)
+        shutil.rmtree('temp')
+        print(
+            f"Detected {len(videos.keys())} videos, {chaptered_video_num} are(is) chaptered.")
+    else:
+        raise FileNotFoundError("Invalid Input Path")