0

Ive been researching all of today and yesterday trying to find a solution, none have worked for me so far.

For example: https://stackoverflow.com/questions/5784661/how-do-you-convert-an-entire-directory-with-ffmpeg

I want to be able to loop over all files in a folder (such as /Music/), find any files in any directory or subdirectory that ends with .flac, then convert those flac files to alac files using this command:

ffmpeg -i "$i" -c copy -acodec alac "${i%.*}.m4a"

the $i variable is the "name" of the file from the for loop. ${i%.*} should just return the name, not the .flac portion.

Anyways, after that command has been run, I want all of those saved .m4a alac files to then be sent to a "new library"... IE: Ill have two /Music/ libraries.

One has all the flac files

One has all the alac (m4a) files

Is this possible?

This is the commmand I have so far, definitely doesnt work, and even if it did, it doesnt create a new library like I want: for i in *.flac; do ffmpeg -i "$i" -c copy -acodec alac "${i%.*}.m4a"; done

Currently that command just throws the error *.flac: No such file or directory, which I assume is because its not searching subdirectories?...

4
  • You may use find. Here is an example: askubuntu.com/a/898121/333867 You need to adapt it from convert to ffmpeg.
    – Hermann
    Aug 28, 2022 at 13:35
  • Use find and maybe xargs. Note: Unix does not have file-name-extensions. It is just a naming convention. (This is also true of MS windows, since Windows-95. But the file-explorer still uses them. ) Aug 28, 2022 at 14:34
  • Paths of converted files in the new directory should be the same as in the original Music directory? For example, an original file path Music/one/two/song.flac should be repeated in New_Music, like New_Music/one/two/song.m4a? Or New_Music should have just heap of files?
    – MiniMax
    Aug 28, 2022 at 15:48
  • matching paths, just a new root folder, such as "Music2" or something Aug 28, 2022 at 17:12

1 Answer 1

2
#!/bin/bash

shopt -s globstar

src_dir="Music"
dest_dir="Music_new"

for orig_path in "${src_dir}"/**/*.flac; do
    #Change first directory in the path
    new_path=${orig_path/$src_dir/$dest_dir}

    #Remove filename from new path. Only directories are left.
    #They are needed for mkdir
    dir_hier=${new_path%/*}

    #Remove extension .flac from path, thus we have original path with first directory
    #changed and file extension removed.
    #Original path:   "Music/one/two/song.flac"
    #New path:        "Music_new/one/two/song"
    new_path=${new_path%.*}

    echo mkdir -p "$dir_hier"

    #New path with extension:   "Music_new/one/two/song.m4a"
    #ffmpeg should create new file by this path, necessary directories
    #already created by mkdir
    echo ffmpeg -i "$orig_path" -c copy -acodec alac "${new_path}.m4a"
done

echo before mkdir and ffmpeg should be removed after checking.

1
  • removed echos, and it appears to work. Aug 28, 2022 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.