Skip to content

Instantly share code, notes, and snippets.

@bennyyip
Created May 23, 2021 12:41
Show Gist options
  • Save bennyyip/de51ee5365a82f20b81b31612a64db69 to your computer and use it in GitHub Desktop.
Save bennyyip/de51ee5365a82f20b81b31612a64db69 to your computer and use it in GitHub Desktop.
import os
import sys
from os.path import getsize, join
from pathlib import Path
from subprocess import DEVNULL, PIPE, Popen
rootpath = sys.argv[1]
audio_suffices = [".mp3", ".flac", ".m4a", ".aac"]
for root, dirs, files in os.walk(rootpath):
if len(files) == 0:
continue
if "cover.jpg" in files:
continue
thefile = None
for x in files:
if Path(x).suffix in audio_suffices:
thefile = x
break
if thefile is None:
continue
ffmpeg_args = [
"ffmpeg",
"-i",
os.path.join(root, thefile),
"-an",
"-vcodec",
"copy",
os.path.join(root, "cover.jpg"),
]
process = Popen(ffmpeg_args, stdout=DEVNULL, stderr=DEVNULL)
(output, err) = process.communicate()
exit_code = process.wait()
if exit_code != 0:
print(f"{root} extract album art failed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment