Skip to content

Instantly share code, notes, and snippets.

@kxfeng
Created November 1, 2024 13:04
Show Gist options
  • Save kxfeng/9fa5641dd205786bb0a0f58499e40b12 to your computer and use it in GitHub Desktop.
Save kxfeng/9fa5641dd205786bb0a0f58499e40b12 to your computer and use it in GitHub Desktop.
Convert videos to Apple compatible videos, to fix photo sync error between Apple devices: "Some videos were not copied to the iPhone because they cannot be played on this iPhone."
import os
import subprocess
import shutil
def convert_videos(input_dir, output_dir):
# Ensure the output directory exists
os.makedirs(output_dir, exist_ok=True)
# Get all mp4 files
mp4_files = [f for f in os.listdir(input_dir) if f.endswith('.mp4')]
total_files = len(mp4_files)
for index, filename in enumerate(mp4_files, start=1):
input_file = os.path.join(input_dir, filename)
output_file = os.path.join(output_dir, filename)
# Output progress
print(f"{index}/{total_files} {filename}")
# Call ffmpeg to perform the conversion
ffmpeg_command = [
'ffmpeg',
'-hide_banner',
'-loglevel', 'error',
'-i', input_file,
'-vcodec', 'libx264',
'-profile:v', 'main',
'-level', '3.1',
'-preset', 'medium',
'-crf', '25',
'-x264-params', 'ref=4',
'-acodec', 'copy',
'-movflags', '+faststart',
output_file
]
subprocess.run(ffmpeg_command)
# Modify output file's timestamp attributes
if os.path.exists(output_file):
input_stat = os.stat(input_file)
os.utime(output_file, (input_stat.st_atime, input_stat.st_mtime))
# For creation time, if the system supports it
try:
shutil.copystat(input_file, output_file)
except Exception as e:
print(f"Unable to copy timestamps: {e}")
if __name__ == "__main__":
input_directory = './input' # Input directory
output_directory = './output' # Output directory
convert_videos(input_directory, output_directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment