Share your projects


Since I can't resist fiddling with things here's a version which doesn't change the working directory.

Python:
# Import required modules
import os, subprocess

# Script folder path
audexfold = os.path.dirname(os.path.abspath(__file__))

# Loop through files in input folder
with os.scandir(os.path.join(audexfold, 'input')) as inpfold:
    for vidfile in inpfold:
        if not vidfile.name.startswith('.') and vidfile.is_file():
            
            # Construct output file path
            fnameparts = os.path.splitext(os.path.basename(vidfile))
            audfile = os.path.join(audexfold, 'output', fnameparts[0] + '.flac')
            
            # Run ffmpeg with specified options
            subprocess.run(['ffmpeg', '-i', vidfile, '-vn', '-f', 'flac', audfile])

Download
 
  • Like
Reactions: rSl
I don't have videos to try this code, some rewrites of the code for the fun of it.
My code was written on a windows box for linux, might not actually work.
Here's the rewritten version.
Code:
import os, subprocess

os.chdir(os.path.dirname(os.path.abspath(__file__)))

for vidfile in [vf for vf in os.scandir('input') if vf.is_file() and not vf.name.startswith('.')]:
    audfile = os.path.join('output', os.path.splitext(vidfile.name)[0] + '.flac')
    subprocess.run(['ffmpeg', '-i', vidfile, '-vn', '-f', 'flac', audfile])

Some try to make the script use multiple cores so speed-up the processing (not sure if this works):
Code:
import os, subprocess, multiprocessing

def process_video(vidfile):
    fnameparts = os.path.splitext(os.path.basename(vidfile))
    audfile = os.path.join('output', fnameparts[0] + '.flac')
    subprocess.run(['ffmpeg', '-i', vidfile, '-vn', '-f', 'flac', audfile])

if __name__ == "__main__":
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    
    with multiprocessing.Pool() as pool:
        inpfold = [vf for vf in os.scandir('input') if vf.is_file() and not vf.name.startswith('.')]
        pool.map(process_video, inpfold)
 
  • Like
Reactions: rSl
When you guys use code::blocks do you use your own makefiles? I'd like to but it seems like a bit of a pain.
 
I do. Found managing the built with CB to be a PITA.
Though I don't use make, but mk (plan9 port or base9 or some sw package like that). And I want to take a closer look on knit, when they reach 1.0.
 
More first world problems.

Python:
with os.scandir('videos') as inp_fold:
    for vid_file in inp_fold:

works fine if the input folder isn't empty, if it is it falls over (if the input folder is empty the vid_file variable doesn't get defined). I've come up with a couple of methods to stop this from happening.

Python:
import os

os.chdir(os.path.dirname(os.path.abspath(__file__)))

# Method one
a = None

with os.scandir('videos') as inp_fold:
    for a in inp_fold:
        print('Processing files.')
    if not a == None:
        print('Files processed.')
    else:
        print('Folder is empty.')

# Method two
with os.scandir('videos') as inp_fold:
    try:
        for b in inp_fold:
            print('Processing files.')
        if not 'b' in globals():
            raise NameError()
        else:
            print('Files processed.')
    except NameError:
        print('Folder is empty.')

Method one:

Predefine the a variable and check its contents after the for loop has finished executing, if the contents have changed the input folder has files in it, if the contents haven't changed the input folder is empty.

Method two:

Check the global/local variable dictionary to see if the b variable exists, if it does the input folder has files in it, if it doesn't the input folder is empty.

I'm tending towards using method one due to it being less computationally intensive.
 
Since folks may be wondering why I seem unwilling to take advice onboard. Its not because I think that what I come up with is better than what other people come up with, its because I don't want people to waste their time on me. My thinking is if it seems like I'm ignoring the advice then people will stop offering it and thereby have more time to spend on more useful things.
 

q4bWJ7Pf.jpg
 
Python:
from os.path import *
from    glob import *

n = 0

for f in glob(dirname(__file__) + '/videos/*'):
  print('++', f)
  n += 1

exit(0 if n else 1)
EDIT: glob import for better indentation
 
Last edited:
Finally finished the final version of my audio extraction program, I've even made a portable version for my friend because he doesn't have Python on his PC. All I need to do now before I post it here is finish writing the readme file, so I'll see you all in 2024.
 
The (hopefully) final version of audex is available to download from my website.

Also as a bonus here's the script that I made to populate the options file.

Python:
import configparser
import os

# Change working directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# Construct options file
opts_parser = configparser.ConfigParser()
opts_parser['folder paths'] = {}
opts_parser['folder paths']['input'] = 'video_files'
opts_parser['folder paths']['output'] = 'audio_files'

with open('options.ini', 'w', encoding = 'utf-8') as opts_file_out:
    opts_parser.write(opts_file_out)

with open('options.ini', 'r', encoding = 'utf-8') as opts_file_inp:
    opts_parser.read_file(opts_file_inp)

# Display contents of options file to check for errors
print(opts_parser.sections())
for opts in opts_parser['folder paths'].items():
    print(opts)

configparser documentation
 
Back
Top