Skip to content

Instantly share code, notes, and snippets.

@mh-firouzjah
Created February 16, 2021 10:12
Show Gist options
  • Save mh-firouzjah/b169c6a67b63c0371f34fa4e47baab22 to your computer and use it in GitHub Desktop.
Save mh-firouzjah/b169c6a67b63c0371f34fa4e47baab22 to your computer and use it in GitHub Desktop.
python-ffmpeg merging video and sound files
# I had mistakenly downloaded some video fiels without sound so then after I had to download the sound files related but
# how could I merge them to finally have a single file? following code would this!
# !attention this file has to be inside the directory where the video and sound exists -
# and the result would be in a sub-directory named `output`
import os
from os import listdir
from os.path import isfile, join
mypath = '.'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
mp4s = [f.split('.')[:-1] for f in onlyfiles if f.split('.')[-1] == 'mp4']
def path_creator(string):
string = string.replace(' ', '\ ')
string = string.replace('#', '\#')
string = string.replace('&', '\&')
return string
os.system('mkdir output')
for f in mp4s:
os.system(
f'ffmpeg -i ./{path_creator(str(*f))}.mp4 -i ./{path_creator(str(*f))}.webm -c copy ./output/{path_creator(str(*f))}.mp4')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment