Created
May 11, 2024 11:47
-
-
Save iamtalhaasghar/6cade2af95a80b73950a5cb09eff3516 to your computer and use it in GitHub Desktop.
A script to convert all .mp4 files in a dir to .mp3 audio files using ffmpeg.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
from glob import glob | |
import time | |
import logging | |
import sys | |
import shutil | |
import requests | |
from sha256sum import sha256sum | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(message)s') | |
stdout_handler = logging.StreamHandler(sys.stdout) | |
stdout_handler.setLevel(logging.DEBUG) | |
stdout_handler.setFormatter(formatter) | |
file_handler = logging.FileHandler('logs.log') | |
file_handler.setLevel(logging.DEBUG) | |
file_handler.setFormatter(formatter) | |
logger.addHandler(file_handler) | |
logger.addHandler(stdout_handler) | |
src = '/mnt/data/bayan-ul-quran-video' | |
dst = '/mnt/data/bayan-ul-quran-audio' | |
usb = '/media/programmer/usb' | |
converted = [] | |
while len(converted) != 108: | |
files = glob(f'{src}/*.mp4') | |
for f in sorted(files): | |
if f not in converted: | |
try: | |
v = os.path.basename(f) | |
a = v.replace('.mp4', '.mp3') | |
out = os.path.join(dst, a) | |
try: | |
if not os.path.exists(out): | |
cmd = f'ffmpeg -i "{f}" "{out}"' | |
logger.info(cmd) | |
os.system(cmd) | |
msg = f'converted {a}' | |
#requests.post('https://ntfy.sh/million_down', data=msg, headers={'Priority': 'high'}) | |
except Exception as e: | |
logger.exception(e) | |
usb_v = os.path.join(usb, 'dr_israr_tafseer_video', v) | |
if os.path.exists(usb_v) and sha256sum(f) == sha256sum(usb_v): | |
logger.info(f'{usb_v} already exists and hash matches with src file') | |
else: | |
logger.info(f'copying {f} to {usb_v}') | |
shutil.copy(f, usb_v) | |
usb_a = os.path.join(usb, 'dr_israr_tafseer_audio', a) | |
if os.path.exists(usb_a) and sha256sum(out) == sha256sum(usb_a): | |
logger.info(f'{usb_a} already exists and hash matches with src file') | |
else: | |
logger.info(f'copying {out} to {usb_a}') | |
shutil.copy(out, usb_a) | |
except Exception as e: | |
logger.exception(e) | |
converted.append(f) | |
time.sleep(5) | |
#requests.post('https://ntfy.sh/million_down', data='shutting down system', headers={'Priority': 'high'}) | |
#os.system('shutdown now') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment