合并'43. DP (Part1)'中的所有视频文件并命名合并文件为文件夹名称
我有一个父文件夹叫做 alpha part 3
,里面有几个子文件夹,分别是 43. DP (Part1)
、44. DP (Part2)
和 45. DP (Part3)
。
我想把 43. DP (Part1)
里面的所有视频文件合并在一起,然后把合并后的文件放回到 43. DP (Part1)
这个文件夹里,并把合并后的文件命名为这个文件夹的名字。
然后对其他的文件夹也做同样的事情。
alpha part 3
├── 43. DP (Part1)
│ ├── _43.1_Introduction to DP.mp4
│ ├── _43.2_What is DP_ (Definition).mp4
│ ├── _43.3_Ways of DP.mp4
│ ├── _43.4_7 Important Concepts.mp4
│ ├── _43.5_Climbing Stairs (Recursion).mp4
│ ├── _43.6_Climbing Stairs (Memoization DP).mp4
│ ├── _43.7_Climbing Stairs Variation.mp4
│ └── _43.8_Climbing Stairs (Tabulation DP).mp4
├── 44. DP (Part2)
│ ├── _44.1_Types of Knapsack problems.mp4
│ ├── _44.2_0-1 Knapsack (Recursion).mp4
│ ├── _44.3_0-1 Knapsack (Memoization).mp4
│ ├── _44.4_0-1 Knapsack (Tabulation).mp4
│ ├── _44.5_Target Sum Subset (Tabulation).mp4
│ ├── _44.6_Target Sum Subset (Code).mp4
│ └── _44.7_Unbounded Knapsack (Tabulation).mp4
├── 45. DP (Part3)
│ ├── _45.1_Coin Change (Live Class).mp4
│ ├── _45.2_Rod Cutting.mp4
│ ├── _45.3_Longest Common Subsequence (Recursion).mp4
│ ├── _45.4_LCS (Memoization).mp4
│ └── _45.5_LCS (Tabulation).mp4
这样做对吗?有没有什么方法可以一次性完成这个操作?请用 Python 和 FFmpeg 写代码。
我的 Python 代码如下;我需要调整什么吗?
import os
import subprocess
# Function to merge videos in a folder
def merge_videos_in_folder(folder_path):
print(f"Merging videos in folder: {folder_path}")
# Use FFmpeg to concatenate all video files within the folder
cmd = [
"ffmpeg",
"-f", "concat",
"-safe", "0",
"-i", "<(find '{}' -type f -name '*.mp4' -exec echo 'file {{}}' \;)".format(folder_path),
"-c", "copy",
os.path.join(folder_path, "merged_video.mp4")
]
subprocess.run(cmd, capture_output=True, text=True)
print(f"Videos merged for folder: {folder_path}")
# Specify the directory containing folders with video files
parent_dir = "/path/to/parent_directory"
# Loop through each folder in the input directory
for folder in os.listdir(parent_dir):
folder_path = os.path.join(parent_dir, folder)
if os.path.isdir(folder_path):
# Merge videos in the current folder
merge_videos_in_folder(folder_path)
print("All videos merged successfully!")
0 个回答
暂无回答