Advertisement
Guest User

Untitled

a guest
May 31st, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1.  
  2.  
  3. import os
  4. import re
  5. import subprocess
  6.  
  7. def find_matching_files(folder_path):
  8. # Regex pattern for DJI files (e.g., DJI_20250530075349_0003_D)
  9. pattern = r"DJI_(\d{14}_\d{4})_D\.(MP4|WAV)"
  10.  
  11. # Dictionaries to store files by their unique identifier
  12. mp4_files = {}
  13. wav_files = {}
  14.  
  15. # Iterate through files in the folder
  16. for filename in os.listdir(folder_path):
  17. match = re.match(pattern, filename)
  18. if match:
  19. identifier = match.group(1) # e.g., 20250530075349_0003
  20. extension = match.group(2).lower()
  21.  
  22. if extension == "mp4":
  23. mp4_files[identifier] = filename
  24. elif extension == "wav":
  25. wav_files[identifier] = filename
  26.  
  27. return mp4_files, wav_files
  28.  
  29. def merge_files(folder_path, mp4_files, wav_files):
  30. # Ensure output directory exists
  31. output_dir = os.path.join(folder_path, "merged_output")
  32. os.makedirs(output_dir, exist_ok=True)
  33.  
  34. # Find matching pairs
  35. for identifier in mp4_files:
  36. if identifier in wav_files:
  37. mp4_path = os.path.join(folder_path, mp4_files[identifier])
  38. wav_path = os.path.join(folder_path, wav_files[identifier])
  39. output_filename = f"DJI_{identifier}_D_merged.MP4"
  40. output_path = os.path.join(output_dir, output_filename)
  41.  
  42. # FFmpeg command to keep original AAC stereo and add WAV stereo as a second stereo track, excluding timecode
  43. ffmpeg_cmd = [
  44. "ffmpeg",
  45. "-i", mp4_path, # Input video file with original stereo audio
  46. "-i", wav_path, # Input WAV file with stereo audio
  47. "-c:v", "copy", # Copy video stream without re-encoding
  48. "-map", "0:v:0", # Map video from MP4
  49. "-map", "0:a:0", # Map AAC stereo audio from MP4 (first audio stream, index 0)
  50. "-c:a:0", "copy", # Copy MP4 AAC audio without re-encoding
  51. "-map", "1:a:0", # Map WAV stereo audio
  52. "-c:a:1", "aac", # Encode WAV audio to AAC
  53. "-b:a:1", "192k", # Set a reasonable bitrate for AAC
  54. "-ac:a:1", "2", # Explicitly set WAV audio to stereo (2 channels)
  55. "-metadata:s:a:0", "title=MP4 Stereo Audio", # Label first audio track
  56. "-metadata:s:a:1", "title=WAV Stereo Audio", # Label second audio track
  57. "-map_chapters", "-1", # Exclude chapters (if any)
  58. "-map_metadata", "-1", # Exclude global metadata (to avoid timecode inclusion)
  59. "-shortest", # Match duration to shortest input
  60. output_path
  61. ]
  62.  
  63. try:
  64. # Run FFmpeg command
  65. result = subprocess.run(ffmpeg_cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
  66. print(f"Successfully merged: {output_filename}")
  67. except subprocess.CalledProcessError as e:
  68. print(f"Error merging {mp4_files[identifier]} and {wav_files[identifier]}: {e.stderr}")
  69.  
  70. def main():
  71. # Get the folder path
  72. folder_path = input("Enter the folder path containing MP4 and WAV files (or press Enter for current directory): ").strip()
  73. if not folder_path:
  74. folder_path = os.getcwd()
  75.  
  76. # Validate folder path
  77. if not os.path.isdir(folder_path):
  78. print("Error: Invalid folder path.")
  79. return
  80.  
  81. # Find matching MP4 and WAV files
  82. mp4_files, wav_files = find_matching_files(folder_path)
  83.  
  84. if not mp4_files or not wav_files:
  85. print("No matching MP4 or WAV files found.")
  86. return
  87.  
  88. # Merge matching files
  89. merge_files(folder_path, mp4_files, wav_files)
  90.  
  91. if __name__ == "__main__":
  92. main()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement