Author Topic: Losslessly cropping Opus files  (Read 4476 times)

sveakul

  • Sr. Member
  • ****
  • Posts: 2438
(Updated 05/20/2019)

There are two ways to go about this I've found, both using ffmpeg:

1.  LosslessCut is an app designed to crop most media files (audio or video) without re-encoding.  While it uses ffmpeg, its format handling is limited by what its Chromium-based HTML5 player will also support.  For Opus, it will handle files that do not include embedded images in their metadata, which means you should remove any album artwork tags first.  LosslessCut allows very precise setting of "start" and "end" times for the cut, and the ability to cue using the file's audio--very helpful when setting cut points "by ear."   Home: https://github.com/mifi/lossless-cut

2.  Using a batch file that calls ffmpeg directly, allowing any Opus file to be losslessly cropped without re-encoding.  The one I use was created by "jaybeee @ themixingbowl.org" and originally posted to another public forum for anyone's use.  It was originally designed to work on every Opus file found in the folder of execution, but I have modified it to work only on a selected file via Windows right-click context menu.  When run it will prompt for the start point of the cut and the end point in hh:mm:ss.mmm format allowing user entry at each time point, display a results summary from ffmpeg, and place the cut file in a "cut-opus" subdirectory of the selected file, with cut file within renamed "(original)-cut.opus."  The source file is retained in its original directory.  To use, copy the code below into a text file and rename with the *.bat extension, e.g. "OpusCut.bat," then place the file in your Windows "SendTo" directory (which normally resides at Users/(username)/Appdata/Roaming/Microsoft/Windows/SendTo).  Now, right-clicking a file and chosing  "SendTo" from the context menu will show a choice for whatever name you gave the .bat file.

The script will work on Opus files that include embedded artwork, but the image tag will be stripped from the cropped result.  Other tag metadata is kept.

Before using the .bat file, open it up in Edit and find the line that allows you to specify the location of ffmpeg.exe on your PC, and modify it to fit your own path.  if you need to get ffmpeg.exe, you can download it at https://ffmpeg.zeranoe.com/builds/ ;  you want the "static" build.

Code
:: Name:     opus-split-ffmpeg.cmd
:: Purpose:  Configures ffmpeg to losslessly split/crop an opus file
:: Author:   jaybeee @ themixingbowl.org
:: Revision: Jan 2017 - v0.1
:: This mod by sveakul for context menu single file use

@ECHO OFF

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:: variables begin with v

:: set name of this script without file extension
SET vMe=%~n0

:: set name of the parent directory where this script resides
SET vParent=%~dp0

:: set location of ffmpeg ** CHANGE ME **
SET vffmpeg="C:\ffmpeg\ffmpeg.exe"


:: Ask for the user to enter the Start & End times in hours, minutes, seconds
echo ** Enter Start Time of where to begin split in hh mm ss.mmm format when prompted **
SET /p vShh="Start Time (hh): "
SET /p vSmm="Start Time (mm): "
SET /p vSss="Start Time (ss.mmm): "

echo ** Enter End Time of where to end split in hh mm ss.mmm format when prompted **
SET /p vEhh="End Time (hh): "
SET /p vEmm="End Time (mm): "
SET /p vEss="End Time (ss.mmm): "

:: convert Start & End input time to seconds
SET /A vStart=(%vShh%*3600)+(%vSmm%*60)+(%vSss)
SET /A vEnd=(%vEhh%*3600)+(%vEmm%*60)+(%vEss)

:: set this variable to Start Time minus 1 second for the fastest seek time
SET /A vFastSeekStart=%vStart%-1

ECHO ffmpeg will now split the file starting at %vStart% seconds and ending at %vEnd% seconds...
ECHO %vFastSeekStart%

:: call ffmpeg to split/crop (copy) out the audio using -ss (start time) & -to (end time) (-t [duration time])
:: time can be: [HH:MM:SS.mmm] eg 01:59:58.123 OR in seconds: [S+.mmm] eg 7198.123
:: Please note: -hide_banner will suppress printing the banner info. Ensure you are running an up to date ffmpeg version
:: -ss %vFastSeekStart%
MKDIR cut-opus
%vffmpeg% -i %1 -ss %vStart% -to %vEnd% -map 0:a -c copy "cut-opus\%~n1-cut.opus"

:: Finish
ECHO Finished opus split

:: pause can be used to view the extraction details
PAUSE

:END
ENDLOCAL
ECHO ON
@EXIT /B 0
Last Edit: May 20, 2019, 07:44:57 PM by sveakul