Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 36

Thread: Recursively convert files with ffmpeg using bash?

  1. #11
    Join Date
    Oct 2013
    Location
    Colorado
    Beans
    560
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: Recursively convert files with ffmpeg using bash?

    Okay, I've determined the script "goes off the rails" if you actually have a directory name ending in ".flac". I also found a minor bug where the script was generating a path with two consecutive slashes. I don't think that hurts, but I'm fixing that also.
    Code:
    inputdir="/home/storage/music/lossless/"
    outputdir="/home/storage/music/compressed/"
    while IFS= read -r file ; do
      song=`basename "$file"`
      dir=`dirname "$file"`
      varyingdir=${dir:${#inputdir}}
      songnotype=${song:0:${#song}-4}
      outputfile="${outputdir}${varyingdir}/${songnotype}m4a"
      # check if output file already exists
      if [ ! -e "$outputfile" ]
      then
        # check if output directory exists
        if [ ! -e "$outputdir$varyingdir" ]
        then
          mkdir -p "$outputdir$varyingdir"
        fi
        avconv -i "$file" -acodec alac "$outputfile"
      fi
    done < <(find "$inputdir" -type f -name "*.flac" | head -10)
    I've bolded the fixes. The first two just remove an extra slash from the directory path being generated. The third change adds a " -type f " qualifier to the find command so that it only selects regular files and not directory names.

  2. #12
    Join Date
    Apr 2007
    Location
    Birdsboro, PA
    Beans
    92
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Recursively convert files with ffmpeg using bash?

    I have made the changes you suggested and modified the command "avconv" to "ffmpeg".

    I'm not sure if there is a change in the output or not. It's not doing what we're expecting, but it seems to possibly be getting further through the directory name? I'm clueless.

    http://pastebin.com/CDN3uitH

    For the sake of additional data sets, I deleted the directories that were created and ran it again.

    http://pastebin.com/2bahYu9G



    For a third run, I set Putty to retain 5000000 lines of scrollback and I had it log the terminal's Printable output.

    This is a link to the entire log: (it was too long for pastebin)



    Hopefully that can assist you in diagnosing what might be going awry.

    I'm probably wrong, but is there a possibility that passing the same "$file" string in lines 4 and 5 :

    Code:
      song=`basename "$file"`
      dir=`dirname "$file"`
    causing the error?

    Seems like that method would require both the dirname and the basename to be identical. Which is not the case. Is it possible that each should have a unique string variable? Totally taking a shot in the dark.


    I am very grateful for your help here. Please don't view my suggestion as anything done in poor taste.

    Thank you!!
    Last edited by madhartigan; July 23rd, 2016 at 08:14 AM. Reason: removed Dropbox link

  3. #13
    Join Date
    Jun 2007
    Beans
    17,337

    Re: Recursively convert files with ffmpeg using bash?

    seems to skip a lot of tracks & create nonsense folders, attaching a log of simple test of last script with the head removed
    2 folders of flac, 1 in sub folder
    Code:
    ~/Music/flac$ ls -R -1A
    rock
    The Rolling Stones-Beggars Banquet
    
    ./rock:
    Jimi Hendrix Experience-Electric Ladyland
    
    ./rock/Jimi Hendrix Experience-Electric Ladyland:
    01.And the Gods Made Love.flac
    02.Have You Ever Been (To Electric Ladyland).flac
    03.Voodoo Chile.flac
    04.Little Miss Strange.flac
    05.Long Hot Summer Night.flac
    06.Come On, Pt. 1.flac
    07.Rainy Day, Dream Away.flac
    08.1983... (A Merman I Should Turn to Be).flac
    09.Moon, Turn the Tides...Gently Gently Away.flac
    10.Still Raining, Still Dreaming.flac
    11.House Burning Down.flac
    Electric Ladyland.m3u
    
    ./The Rolling Stones-Beggars Banquet:
    01.Sympathy For The Devil.flac
    02.No Expectations.flac
    03.Dear Doctor.flac
    04.Parachute Woman.flac
    05.Jig-Saw Puzzle.flac
    06.Street Fighting Man.flac
    07.Prodigal Son.flac
    08.Stray Cat Blues.flac
    09.Factory Girl.flac
    10.Salt Of The Earth.flac
    Beggars Banquet.m3u
    What is produced -
    Code:
    ~/Music/compressed$ ls -R -1A
    ck
    e Rolling Stones-Beggars Banquet
    rock
    The Rolling Stones-Beggars Banquet
    
    ./ck:
    Jimi Hendrix Experience-Electric Ladyland
    
    ./ck/Jimi Hendrix Experience-Electric Ladyland:
    
    ./e Rolling Stones-Beggars Banquet:
    
    ./rock:
    Jimi Hendrix Experience-Electric Ladyland
    
    ./rock/Jimi Hendrix Experience-Electric Ladyland:
    01.And the Gods Made Love.m4a
    09.Moon, Turn the Tides...Gently Gently Away.m4a
    11.House Burning Down.m4a
    
    ./The Rolling Stones-Beggars Banquet:
    02.No Expectations.m4a
    10.Salt Of The Earth.m4a
    Attached Files Attached Files

  4. #14
    Join Date
    Oct 2013
    Location
    Colorado
    Beans
    560
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: Recursively convert files with ffmpeg using bash?

    Time for a little debugging. After the line that reads
    Code:
    outputfile="${outputdir}${varyingdir}/${songnotype}m4a"
    Please add the following lines
    Code:
    echo "file: $file" >> ~/flactoalac.log
    echo "song: $song" >> ~/flactoalac.log
    echo "dir: $dir" >> ~/flactoalac.log
    echo "varyingdir: $varyingdir" >> ~/flactoalac.log
    echo "outputfile: $outputfile" >> ~/flactoalac.log
    Post the flactoalac.log file and that should tell me where names are getting mangled.

  5. #15
    Join Date
    Apr 2007
    Location
    Birdsboro, PA
    Beans
    92
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Recursively convert files with ffmpeg using bash?

    Here ya go!

    Code:
    file: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d1_10_Party_Time.flac
    song: ph160715d1_10_Party_Time.flac
    dir: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: phish/2016-07-15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-07-15 George, WA (FLAC)/ph160715d1_10_Party_Time.m4a
    file: sic/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d1_13_Rift.flac
    song: ph160715d1_13_Rift.flac
    dir: sic/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir:  George, WA (FLAC)
    outputfile: /home/storage/music/compressed/ George, WA (FLAC)/ph160715d1_13_Rift.m4a
    file: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d3_05_Wilson.flac
    song: ph160715d3_05_Wilson.flac
    dir: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: phish/2016-07-15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-07-15 George, WA (FLAC)/ph160715d3_05_Wilson.m4a
    file: /storage/music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d2_03_Whats_The_Use.flac
    song: ph160715d2_03_Whats_The_Use.flac
    dir: /storage/music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: /2016-07-15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed//2016-07-15 George, WA (FLAC)/ph160715d2_03_Whats_The_Use.m4a
    file: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d1_05_Bouncing_Around_The_Room.flac
    song: ph160715d1_05_Bouncing_Around_The_Room.flac
    dir: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: phish/2016-07-15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-07-15 George, WA (FLAC)/ph160715d1_05_Bouncing_Around_The_Room.m4a

  6. #16
    Join Date
    Apr 2007
    Location
    Birdsboro, PA
    Beans
    92
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Recursively convert files with ffmpeg using bash?

    . . . and the output after running it a second time (different result):

    Code:
    file: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d1_10_Party_Time.flac
    song: ph160715d1_10_Party_Time.flac
    dir: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: phish/2016-07-15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-07-15 George, WA (FLAC)/ph160715d1_10_Party_Time.m4a
    file: usic/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d1_13_Rift.flac
    song: ph160715d1_13_Rift.flac
    dir: usic/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: 5 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/5 George, WA (FLAC)/ph160715d1_13_Rift.m4a
    file: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d3_05_Wilson.flac
    song: ph160715d3_05_Wilson.flac
    dir: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: phish/2016-07-15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-07-15 George, WA (FLAC)/ph160715d3_05_Wilson.m4a
    file: /music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d2_03_Whats_The_Use.flac
    song: ph160715d2_03_Whats_The_Use.flac
    dir: /music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: -15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/-15 George, WA (FLAC)/ph160715d2_03_Whats_The_Use.m4a
    file: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d1_05_Bouncing_Around_The_Room.flac
    song: ph160715d1_05_Bouncing_Around_The_Room.flac
    dir: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: phish/2016-07-15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-07-15 George, WA (FLAC)/ph160715d1_05_Bouncing_Around_The_Room.m4a

  7. #17
    Join Date
    Oct 2013
    Location
    Colorado
    Beans
    560
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: Recursively convert files with ffmpeg using bash?

    Looks like the handoff between find and read is not always working. Give me a little while to research how to bulletproof those commands.

    I hate to say "it works for me", but I have 15,290 flac files with all sorts of special characters in the names and it looks like it handled the directory and file names properly. I didn't actually convert them, but I did run all the names through and logged the same info about variables you just provided.

  8. #18
    Join Date
    Oct 2013
    Location
    Colorado
    Beans
    560
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: Recursively convert files with ffmpeg using bash?

    Let's try this. Change the first line to
    Code:
    while IFS= read -r -d $'\0' file ; do
    and the last line to
    Code:
    done < <(find "$inputdir" -type f -name '*.flac' -print0)
    Leave the debug lines in and, if it still doesn't work, post the log file.

  9. #19
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Recursively convert files with ffmpeg using bash?

    Hi madhartigan,

    The variable 'file' is not being read correctly. I would assume there are some non graphic character in some of the filenames or dir names. I'd recommend using this structure to navigate the tree directory:
    Code:
    while IFS= read -r -d '' file
    do
        # do something with file
        echo $file
    done< <(find -type f -name '*.txt' -print0)
    Hope it helps. Let us know how it goes.
    Regards.

  10. #20
    Join Date
    Apr 2007
    Location
    Birdsboro, PA
    Beans
    92
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Recursively convert files with ffmpeg using bash?

    Thank you both for your replies.

    I'm seeing the suggestion from pabibe as being analogous to what Keith is suggesting.

    I am not sure how to incorporate the suggestion from pabibe, so I stuck with modifying flactoalac as Keith suggested. Hopefully this output can provide both of you with useful information.


    Code:
    file: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)/ph160715d1_10_Party_Time.flac
    song: ph160715d1_10_Party_Time.flac
    dir: /home/storage/music/lossless/phish/2016-07-15 George, WA (FLAC)
    varyingdir: phish/2016-07-15 George, WA (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-07-15 George, WA (FLAC)/ph160715d1_10_Party_Time.m4a
    file: flac
    song: flac
    dir: .
    varyingdir: 
    outputfile: /home/storage/music/compressed//m4a
    file: /home/storage/music/lossless/phish/2016-06-22 St. Paul, MN (FLAC)/ph160622d1_08_Round_Room.flac
    song: ph160622d1_08_Round_Room.flac
    dir: /home/storage/music/lossless/phish/2016-06-22 St. Paul, MN (FLAC)
    varyingdir: phish/2016-06-22 St. Paul, MN (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-06-22 St. Paul, MN (FLAC)/ph160622d1_08_Round_Room.m4a
    file: A (FLAC)/ph160629d2_01_Crosseyed_And_Painless.flac
    song: ph160629d2_01_Crosseyed_And_Painless.flac
    dir: A (FLAC)
    varyingdir: 
    outputfile: /home/storage/music/compressed//ph160629d2_01_Crosseyed_And_Painless.m4a
    file: /home/storage/music/lossless/phish/2016-06-29 Philadelphia, PA (FLAC)/ph160629d2_08_Backwards_Down_The_Number_Line.flac
    song: ph160629d2_08_Backwards_Down_The_Number_Line.flac
    dir: /home/storage/music/lossless/phish/2016-06-29 Philadelphia, PA (FLAC)
    varyingdir: phish/2016-06-29 Philadelphia, PA (FLAC)
    outputfile: /home/storage/music/compressed/phish/2016-06-29 Philadelphia, PA (FLAC)/ph160629d2_08_Backwards_Down_The_Number_Line.m4a
    file: Hartford, CT [FLAC]/ph160709d2_03_Sand.flac
    song: ph160709d2_03_Sand.flac
    dir: Hartford, CT [FLAC]
    varyingdir: 
    outputfile: /home/storage/music/compressed//ph160709d2_03_Sand.m4a
    file: /home/storage/music/lossless/phish/2016-07-09 Xfinity Theatre, Hartford, CT [FLAC]/ph160709d1_07_Let_Me_Lie.flac
    song: ph160709d1_07_Let_Me_Lie.flac
    dir: /home/storage/music/lossless/phish/2016-07-09 Xfinity Theatre, Hartford, CT [FLAC]
    varyingdir: phish/2016-07-09 Xfinity Theatre, Hartford, CT [FLAC]
    outputfile: /home/storage/music/compressed/phish/2016-07-09 Xfinity Theatre, Hartford, CT [FLAC]/ph160709d1_07_Let_Me_Lie.m4a
    file: Bill Graham Civic Auditorium, San Fransisco, CA [FLAC]/ph160718d2_04_My_Sweet_One.flac
    song: ph160718d2_04_My_Sweet_One.flac
    dir: Bill Graham Civic Auditorium, San Fransisco, CA [FLAC]
    varyingdir:  San Fransisco, CA [FLAC]
    outputfile: /home/storage/music/compressed/ San Fransisco, CA [FLAC]/ph160718d2_04_My_Sweet_One.m4a
    file: /home/storage/music/lossless/phish/2016-07-18 Bill Graham Civic Auditorium, San Fransisco, CA [FLAC]/ph160718d1_02_Halleys_Comet.flac
    song: ph160718d1_02_Halleys_Comet.flac
    dir: /home/storage/music/lossless/phish/2016-07-18 Bill Graham Civic Auditorium, San Fransisco, CA [FLAC]
    varyingdir: phish/2016-07-18 Bill Graham Civic Auditorium, San Fransisco, CA [FLAC]
    outputfile: /home/storage/music/compressed/phish/2016-07-18 Bill Graham Civic Auditorium, San Fransisco, CA [FLAC]/ph160718d1_02_Halleys_Comet.m4a

    Once the script got to that last file, it hanged and left me with the following:

    Code:
    Enter command: <target>|all <time>|-1 <command>[ <argument>]
    ^Z
    [3]+  Stopped                 ./flactoalac
    I pressed "Break" to get back to the command prompt.

    So you can verify I modified the script properly:

    Code:
    darryl@helios:~$ cat flactoalac
    #!/bin/bash
    
    inputdir="/home/storage/music/lossless/"
    outputdir="/home/storage/music/compressed/"
    while IFS= read -r -d $'\0' file ; do
      song=`basename "$file"`
      dir=`dirname "$file"`
      varyingdir=${dir:${#inputdir}}
      songnotype=${song:0:${#song}-4}
      outputfile="${outputdir}${varyingdir}/${songnotype}m4a"
      echo "file: $file" >> ~/flactoalac.log
      echo "song: $song" >> ~/flactoalac.log
      echo "dir: $dir" >> ~/flactoalac.log
      echo "varyingdir: $varyingdir" >> ~/flactoalac.log
      echo "outputfile: $outputfile" >> ~/flactoalac.log
      # check if output file already exists
      if [ ! -e "$outputfile" ]
      then
        # check if output directory exists
        if [ ! -e "$outputdir$varyingdir" ]
        then
          mkdir -p "$outputdir$varyingdir"
        fi
        ffmpeg -i "$file" -acodec alac "$outputfile"
      fi
    done < <(find "$inputdir" -type f -name '*.flac' -print0)
    darryl@helios:~$


    Thank you both, very much, for your time and suggestions.

Page 2 of 4 FirstFirst 1234 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •