Jump to content

mp3 file pitch and speed


Recommended Posts

i have a script that is supposed to edit the pitch and speed and save as a mp3 like the input mp3

it gives no result, nothing saves the input file

 

#include <File.au3>
#include <Array.au3>
#include <Constants.au3>

; Set the path to the FFmpeg executable
Global $ffmpegPath = "C:\Users\user\Downloads\ffmpeg-master-latest-win64-gpl\ffmpeg-master-latest-win64-gpl\bin\ffmpeg.exe"

; Set the path to the folder containing the audio files
Global $audioFolder = "C:\Users\user\Downloads\mp3\mp3"

; Set the desired pitch and speed adjustments
Global $pitchAdjustment = 0.72 ; Increase pitch by 50%
Global $speedAdjustment = 0.83 ; Decrease speed by 50%

; Recursively search for all MP3 files in the specified folder and its subfolders
Global $mp3Files = _FileListToArrayRec($audioFolder, "*.mp3", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT)

; Loop through each MP3 file and apply the pitch and speed adjustments using FFmpeg
For $i = 1 To $mp3Files[0]
    ; Get the full path to the current MP3 file
    Local $mp3File = $mp3Files[$i]

    ; Get the path to the folder containing the current MP3 file
    Local $mp3Folder = StringLeft($mp3File, StringInStr($mp3File, "\", 0, -1))

    ; Get the base filename (without extension) of the current MP3 file
    Local $mp3BaseName = StringRegExpReplace($mp3File, "(.*\\)(.*)(\.mp3)", "$2")

    ; Construct the command-line arguments for FFmpeg
    Local $ffmpegArgs = "-i """ & $mp3File & """ -filter:a ""atempo=" & $speedAdjustment & ",asetrate=44100*" & $pitchAdjustment & """ -vn -c:a libmp3lame -q:a 2 """ & $mp3Folder & $mp3BaseName & ".mp3"""

    ; Call FFmpeg to process the current MP3 file
    RunWait('"' & $ffmpegPath & '" ' & $ffmpegArgs, "", @SW_HIDE)
Next

how to fix it

mp3.rar

Link to comment
Share on other sites

Yep, ran your code and it processed just fine. It created a separate file however, not overwriting the original. You have no logging, likely you have something wrong, like no files in the folder you set, or wrong ffmpeg directory. I would suggest that you add some form of logging to each line and figure out where it all goes wrong. Also set your RunWait to @SW_SHOW and make sure that it's actually running. It processes pretty quick and closes pretty fast, so you may want to look into logging the output of ffmpeg with StdoutRead: https://www.autoitscript.com/autoit3/docs/functions/StdoutRead.htm

 

Additionally, your regex doesn't appear to work here: 

Local $mp3BaseName = StringRegExpReplace($mp3File, "(.*\\)(.*)(\.mp3)", "$2")

I think maybe you want: (.*\\)?(.*)(\.mp3)

Because it doesn't work, it saves the output as 318.mp3.mp3 as you can see in @argumentum's command line. Although I would recommend probably checking out _PathSplit instead: https://www.autoitscript.com/autoit3/docs/libfunctions/_PathSplit.htm This is likely a safer option than RegEx if you're not super familiar with it. This can also help with $mp3Folder

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...