Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Friday, October 2, 2020

How to trim audio (MP3) files

Summary: PowerShell script to trim beginning and end of the audio (MP3, etc) files.
The following PowerShell script will remove the specified number of seconds from the beginning and/or end of every audio file with the given extension (.mp3 in this case) under the specified folder and all subfolders unerneath (requires FFmpeg binaries):
# Input folder holding audio files.
$inputDir = "Z:\Lectures"

# Seconds to trim from beginning of file.
$trimStart = 0.0

# Seconds to trim from the end of file.
$trimEnd = 9.0

# Path to the directory holding FFMPEG tools.
$ffmpegDir = "c:\ffmpeg\bin"

# Extension of the audio files.
$ext = ".mp3"

# Extension for temporary files.
$tmpExt = ".TMP$ext"

# Paths to FFMPEG tools.
$ffmpeg  = Join-Path $ffmpegDir "ffmpeg.exe"
$ffprobe = Join-Path $ffmpegDir "ffprobe.exe"

# Process all audio files in the directory and subdirectories.
Get-ChildItem -LiteralPath $inputDir -Filter "*$ext" -Recurse -File | ForEach-Object {
    # Original file path.
    $oldFile = $_.FullName

    # Original file name.
    $oldName = $_.Name

    # Temp file path will be in the same folder named after the original file.
    $tmpFile = "$oldFile$tmpExt"

    # Get the length of the audio track (it's a sting holding a floating number with possible new line).
    $duration = (& $ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1 $oldFile) | Out-String

    $duration = $duration.Trim()

    # Set new length of the audio by removing the trimmed parts.
    $duration -= ($trimEnd + $trimStart)

    # Trim the file.
    & $ffmpeg -ss $trimStart -t $duration -i $oldFile -acodec copy $tmpFile

    # Delete the original file.
    Remove-Item -LiteralPath $oldFile -Force

    # Rename the temp file to the original.
    Rename-Item -LiteralPath $tmpFile $oldName -Force
}

Tuesday, August 22, 2017

How to clean up BIN and OBJ folders in a Visual Studio solution

Summary: Some ideas on cleaning up intermediate and output folders in Visual Studio projects.
There may be better (and more elegant) ways of cleaning up the output (BIN) and intermediate (OBJ) folders generated by Visual Studio build process, but the following script is probably the easiest option you can use:

@echo off
rem Delete BIN and OBJ folders from the immediate folder and all subfolders.
rem

rem Switch to the script folder.
cd "%~dp0"

rem Use the following to suppress the 'File Not Found' message if no folders are found.
setlocal enabledelayedexpansion 
for /f "tokens=*" %%G in ('dir /B /AD /S bin 2^>nul') do rmdir /S /Q "%%G"
for /f "tokens=*" %%G in ('dir /B /AD /S obj 2^>nul') do rmdir /S /Q "%%G"

Notice that the script call setlocal enabledelayedexpansion to allow the 2^>nul redirection in the for loops (without it, it would output the "File Not Found" message if the folder and subfolders do not hold the "BIN" or "OBJ" folders. Also, make sure, you place the file in the root of the solution folder.

See also:
How to clean Visual Studio bin and obj folders
I want to delete all bin and obj folders to force all projects to rebuild everything

Thursday, April 21, 2016

Generate filename-friendly datetime in Windows shell script

Summary: Windows batch script to generate timestamp for a filename.
People who write Windows Shell (AKA batch or .BAT) scripts for living sometimes need to create file (or directory) names using timestamp values based on local current date and time. This is not as trivial as it may sound. First, there is no shell command that would return a timestamp in a custom format, and the standard command may return a value containing illegal (for filenames) characters, such as colons.

There are articles that address this issue, but many proposed solutions do not accommodate region specifics, so they may work for an OS configured for one region, but fail for another.

The following script will generate a filename-friendly local timestamp for any region:
@echo off
rem -----------------------------------------------------------------
rem MAIN routine
setlocal & pushd

call :GET_TIMESTAMP
set timestamp=%ret%

echo %timestamp%

rem End of the main routine.
popd & endlocal 
goto :EOF

rem -----------------------------------------------------------------
rem Generate current timestamp in the format:
rem
rem YYYYMMDDhhmmss (as shown here), or
rem YYYYMMDD_hhmmss (commented out), or
rem YYYMMDDhhmmssmmm (commented out), or
rem YYYMMDD_hhmmss_mmm (commented out)
rem
rem Uncomment the block that generates the desired format and comment
rem out the alternate implementations.
rem
rem Returns %ret%.
:GET_TIMESTAMP
setlocal

for /f "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /value 2^>NUL`) do (
if '.%%i.'=='.LocalDateTime.' set ldt=%%j
)
rem Without milliseconds, without underscore: 20160421140125
set timestamp=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%%ldt:~8,2%%ldt:~10,2%%ldt:~12,2%
rem echo %timestamp%

rem Without milliseconds, with underscore: 20160421_140125
rem timestamp=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%_%ldt:~8,2%%ldt:~10,2%%ldt:~12,2%
rem echo %timestamp%

rem With milliseconds, without underscores: 20160421140125202
rem set timestamp=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%%ldt:~8,2%%ldt:~10,2%%ldt:~12,2%%ldt:~15,3%
rem echo %timestamp%

rem With milliseconds, with underscores: 20160421_140125_202
rem set timestamp=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%_%ldt:~8,2%%ldt:~10,2%%ldt:~12,2%_%ldt:~15,3%
rem echo %timestamp%

endlocal&set ret=%timestamp%
goto :EOF
Enjoy!

See also:
How to get current datetime on Windows command line, in a suitable format for using in a filename?