Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script, which convert mov, flv, avi .etc to mp4 format
import subprocess
import os
src = './src'
dst = './mp4'
for root, dirs, filenames in os.walk(src, topdown=False):
#print(filenames)
for filename in filenames:
print('[INFO] 1',filename)
try:
_format = ''
if ".flv" in filename.lower():
_format=".flv"
if ".mp4" in filename.lower():
_format=".mp4"
if ".avi" in filename.lower():
_format=".avi"
if ".mov" in filename.lower():
_format=".mov"
inputfile = os.path.join(root, filename)
print('[INFO] 1',inputfile)
outputfile = os.path.join(dst, filename.lower().replace(_format, ".mp4"))
subprocess.call(['ffmpeg', '-i', inputfile, outputfile])
except:
print("An exception occurred")
@CalvoM
Copy link

CalvoM commented Jul 11, 2022

Is there an issue you encountered while using the ffmpeg python package?

@bdewhirst
Copy link

bdewhirst commented Jul 16, 2022

Is there an issue you encountered while using the ffmpeg python package?

Disclaimer: I'm just another random person from the internet, I have no connection to this project.

Hi,

I believe ffmpeg is another program (available for Linux, Windows and Mac), and you can find code for it elsewhere on github as well as linked builds.

If you install or identify 'ffmpeg' to your OS, the above script will work as intended. Alternatively, if you replace 'ffmpeg' with a path to the executable (e.g. "C:\\Users\\[username]\\Downloads\\ffmpeg-[version-specific-substring]-full_build\\bin\\ffmpeg.exe"), that works too.

In case it isn't obvious, the above script expects certain folders to exist (specified on lines 4 and 5), and the for loop (lines 7-27) works through all the source files and converts them in the destination folder.

It'll take a while to run, so I suggest trying it with a smaller file (or smaller number of files) until you understand what the script expects.

@bdewhirst
Copy link

bdewhirst commented Jul 16, 2022

Ah, I see there is a ffmpeg python package, so you probably knew all that and were asking why the author wasn't using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment