[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] [已解决]批处理如何获取mp3歌曲时间长度并算出1/4和3/4处的时间点?

如何用批处理获取一首mp3(或批量获取同一文件夹里的多个mp3)的歌曲时间长度并算出1/4和3/4处的时间点?
主要是想批量分割多个mp3成前1/4和后3/4的片段。

我参看国外的mp3相关编辑论坛有分割的,但是目前只能手动指定时间点来分割。
现在很不想手动输入时间点,自动能判断算出那2个时间点甚至多个时间点该是多方便啊,因为每个mp3的时间长度都不一样。

dpmp3split.cmd
  1. @echo off
  2. set "mp3DirectCut=C:\Program Files (x86)\mp3DirectCut_2.20\mp3DirectCut.exe"  
  3. for %%f in (%3) do (
  4.     echo FILE "%%~f" MP3> sheet.cue
  5.     echo TRACK 01 AUDIO>>sheet.cue
  6.     echo INDEX 01 00:00:00>>sheet.cue
  7.     echo TRACK 02 AUDIO>>sheet.cue
  8.     echo INDEX 01 %1>>sheet.cue
  9.     echo TRACK 03 AUDIO>>sheet.cue
  10.     echo INDEX 01 %2>>sheet.cue
  11.     "%mp3DirectCut%" sheet.cue /split
  12.     del *02.mp3
  13. )
  14. del sheet.cue
  15. ren *01.mp3 *" "p01.mp3
  16. ren *03.mp3 *" "p02.mp3
复制代码
用这个分割片段.bat 来调用
  1. ::在这里手动设置截取片段的两个时间点↓
  2. dpmp3split.cmd 01:10:00 03:30:00 *.mp3
复制代码
需要安装 mp3DirectCut 2.20 这个编辑mp3的工具,到处有下。

这里有这个软件的命令行代码
Command line options

Usage:
mp3DirectCut.exe <filename> <option(s)> <destination>

As <filename> every file type known by mp3DirectCut is possible (MP3, MP2, Project files, Cue sheets). Usually this is simply for a file to be opened. In your operating system settings you can associate mp3DirectCut to one or more of these file types and with different options. Then you can open the program easily by any file of that type. It is also possible to drag files of known types over mp3DirectCut.

If you use the <filename> parameter together with /rec you can set an individual record filename that differs from the base filename of the recording settings.

The following options can be used to let the program perform an action immediately. After finnishing, the program quits.

Note: adjustments for Normalize, Auto cue, Encoder settings and so on cannot be given over the command line. They must be made before in the normal program environment.

Examples:
mp3DirectCut "artist - song.mp3"
mp3DirectCut track01.mp3 /normalize /overwrite
mp3DirectCut audiobook.mp3 /autocue d:\split
mp3DirectCut album.cue /split
mp3DirectCut /rec
mp3DirectCut other_recname.mp3 /rec

Options:
/normalize        Normalizes the file up to 0 dB. Higher or lower levels cannot be set in the command line.
/autocue        Splits the given file into segments of the time set in the Autocue dialog. Also for the Split operation the current settings from normal program usage are applied. <destination> can be an existing folder to save the split files to.
/autocrop        Cuts silences from beginning and end of track using the "Lowest gain" value which can be set in the configuration window or in the gain dialog.
/split        Needs a Cue sheet or Project file and splits the audio file(s). If a <destination> folder is set, the split files will be saved there. The other Split settings must be adjusted in the normal program environment.
/pausesplit        Runs the pause detector and splits the given file at the found pause positions. For the performed Pause detection and Save split operations, the current settings are applied. You can change them by normal program usage. <destination> can be an existing folder for the result files.
/save        Opens and saves the file. Simple re-save can be used to eliminate incomplete frames or unwanted headers.
/overwrite        Replaces the original file on /save or /normalize. The current "Keep date of source file" setting is valid (it can be set in the program environment). If /overwrite is not set, the given file extension will be appended once more.
/rec        Starts the recording immediately. This feature is especially made for timer recordings. In a scheduler usually you can also set a duration time after which the program will be terminated. For multiple timer recordings be sure to check Add date and time or Append data in the recording settings, otherwise previous recordings will be overwritten. If a filename is given before the /rec option and Add date and time or Append data is not (!) selected, the program records into a file of that name.
/localini        Regulary the program tries to write the settings into its own folder. If this fails, e.g. because of missing rights, the settings are stored in the user's Application data folder. This option prevents the program from reading from or writing to the user profile. This may be useful in special cases, e.g. if local settings are needed when the program is running from a removable drive.


有没有大触手可以把这2个批处理融合成一个文件,我确实太新手了...


关于获取mp3时间长度,我网上看到
BAT和JScript混合编程获取mp3音频文件的播放时间长度
http://bbs.bathome.net/viewthread.php?tid=29969#pid147781

谢谢高手来相助!
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

本帖最后由 pendave 于 2015-1-28 17:52 编辑

回复 8# apang

我想用你那个JScript代码获取mp3的比特率
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsrch.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP+TechHome/WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP
改了下
  1. b = WMP.newMedia(f).GetItemInfo("Bitrate");
复制代码
可是报错:Microsoft JScript 编译错误:无效字符

怎么回事呢?

想用进这个需求里
[求助]如何用ffmpeg来批量把比特率大于192kbs的mp3文件转成128kbs的?
http://www.bathome.net/viewthrea ... mp;extra=#pid161699

谢谢!

TOP

本帖最后由 pendave 于 2015-1-28 01:57 编辑

搞定了,一点点调试后发现问题了

解决了!谢谢
  1. @set @n=0; /* & echo off
  2. set "JS=cscript -nologo -e:jscript"
  3. set "mp3DirectCut=C:\Program Files (x86)\mp3DirectCut_2.20\mp3DirectCut.exe"
  4. for /f "tokens=1,2*" %%a in ('dir /b *.mp3^|%JS% "%~0"') do (
  5.         set "Name=%%c"
  6.         set /a h1=%%a/3600,m1=%%a%%3600/60,s1=%%a%%3600%%60
  7.         set /a h2=%%b/3600,m2=%%b%%3600/60,s2=%%b%%3600%%60
  8.         set /a h1+=100,m1+=100,s1+=100,h2+=100,m2+=100,s2+=100
  9.         setlocal enabledelayedexpansion
  10.         rem set "t1=!h1:~1!:!m1:~1!:!s1:~1!"
  11.         rem set "t2=!h2:~1!:!m2:~1!:!s2:~1!"
  12.         set "t1=!m1:~1!:!s1:~1!"
  13.         set "t2=!m2:~1!:!s2:~1!"
  14.         echo,!t1! !t2! !Name!
  15.     echo FILE "!Name!"> sh.cue
  16.     echo TRACK 01 AUDIO>>sh.cue
  17.     echo INDEX 01 00:00>>sh.cue
  18.     echo TRACK 02 AUDIO>>sh.cue
  19.     echo INDEX 01 !t1!>>sh.cue
  20.     echo TRACK 03 AUDIO>>sh.cue
  21.     echo INDEX 01 !t2!>>sh.cue
  22.      
  23.     "%mp3DirectCut%" "!Name!" /normalize /overwrite
  24.     "%mp3DirectCut%" sh.cue /split C:\Temp
  25.     "%mp3DirectCut%" C:\Temp\*.mp3 /autocrop
  26.    
  27.         endlocal
  28. )
  29. del "C:\Temp\*Track 02.mp3" sh.cue
  30. ren "C:\Temp\*Track 01.mp3" "* p1.mp3"
  31. ren "C:\Temp\*Track 03.mp3" "* p2.mp3"
  32. pause & exit/b & rem */
  33. WMP = new ActiveXObject("WMPlayer.OCX");
  34. s = "";
  35. while (!WScript.StdIn.AtEndOfStream) {
  36.         f = WScript.StdIn.ReadLine();
  37.         t = WMP.newMedia(f).getItemInfo("Duration");
  38.         t1 = Math.round(t/4);
  39.         t2 = Math.round(t*3/4);
  40.         s += t1 + " " + t2 + " " + f + "\n";
  41. }
  42. WScript.StdOut.Write(s);
复制代码

TOP

  1. @set @n=0; /* & echo off
  2. pushd "F:\Music\"
  3. set "mp3DirectCut=D:\Program Files\mp3DirectCut\mp3DirectCut.exe"
  4. set "JS=cscript -nologo -e:jscript"
  5. for /f "tokens=1,2*" %%a in ('dir /b *.mp3^|%JS% "%~0"') do (
  6.         set "Name=%%c"
  7.         set /a m1=%%a/60+100,s1=%%a%%60+100
  8.         set /a m2=%%b/60+100,s2=%%b%%60+100
  9.         setlocal enabledelayedexpansion
  10.         (
  11.                 echo FILE "!Name!" MP3
  12.                 echo TRACK 01 AUDIO
  13.                 echo INDEX 01 00:00:00
  14.                 echo TRACK 02 AUDIO
  15.                 echo INDEX 01 !m1:~1!:!s1:~1!:00
  16.                 echo TRACK 03 AUDIO
  17.                 echo INDEX 01 !m2:~1!:!s2:~1!:00
  18.          )>sheet.cue
  19.         "%mp3DirectCut%" sheet.cue /split
  20.         endlocal
  21. )
  22. del "*Track 02.mp3" sheet.cue
  23. ren "*Track 03.mp3" "*Track 02.mp3"
  24. pause & exit/b & rem */
  25. WMP = new ActiveXObject("WMPlayer.OCX");
  26. s = "";
  27. while (!WScript.StdIn.AtEndOfStream) {
  28.         f = WScript.StdIn.ReadLine();
  29.         t = WMP.newMedia(f).getItemInfo("Duration");
  30.         t1 = Math.round(t/4)
  31.         t2 = Math.round(t*3/4)
  32.         s += t1 + " " + t2 + " " + f + "\n"
  33. }
  34. WScript.StdOut.Write(s)
复制代码
1

评分人数

TOP

回复 3# apang

我现在代码整合成这样,可是运行就报错无法生成那个 sh.cue 文件啊
  1. @set @n=0; /* & echo off
  2. set "JS=cscript -nologo -e:jscript"
  3. set "mp3DirectCut=C:\Program Files (x86)\mp3DirectCut_2.20\mp3DirectCut.exe"
  4. for /f "tokens=1,2*" %%a in ('dir /b *.mp3^|%JS% "%~0"') do (
  5.         set "Name=%%c"
  6.         set /a h1=%%a/3600,m1=%%a%%3600/60,s1=%%a%%3600%%60
  7.         set /a h2=%%b/3600,m2=%%b%%3600/60,s2=%%b%%3600%%60
  8.         set /a h1+=100,m1+=100,s1+=100,h2+=100,m2+=100,s2+=100
  9.         setlocal enabledelayedexpansion
  10.         set "t1=!m1:~1!:!s1:~1!"
  11.         set "t2=!m2:~1!:!s2:~1!"
  12.         echo,!t1! !t2! !Name!
  13.     echo FILE !Name!> sh.cue
  14.     echo TRACK 01 AUDIO>>sh.cue
  15.     echo INDEX 01 !t1!>>sh.cue
  16.     echo TRACK 02 AUDIO>>sh.cue
  17.     echo INDEX 01 !t2!>>sh.cue
  18.     "%mp3DirectCut%" sh.cue /split C:\Temp
  19.     del C:\Temp\*02.mp3
  20.     del sh.cue
  21.         endlocal
  22. )
  23. pause & exit/b & rem */
  24. WMP = new ActiveXObject("WMPlayer.OCX");
  25. s = "";
  26. while (!WScript.StdIn.AtEndOfStream) {
  27.         f = WScript.StdIn.ReadLine();
  28.         t = WMP.newMedia(f).getItemInfo("Duration");
  29.         t1 = Math.round(t/4);
  30.         t2 = Math.round(t*3/4);
  31.         s += t1 + " " + t2 + " " + f + "\n";
  32. }
  33. WScript.StdOut.Write(s);
复制代码
高手请帮我看看是哪个逻辑出问题了....

!Name! 只是mp3文件的一个属性:文件名? 不是mp3文件本身?

TOP

本帖最后由 pendave 于 2015-1-28 00:00 编辑

回复 3# apang

亲,调用第三方我那个是个For遍历吧,我弄了半天都整合不好,晕死了
  1. @set @n=0; /* & echo off
  2. set "JS=cscript -nologo -e:jscript"
  3. set "mp3DirectCut=C:\Program Files (x86)\mp3DirectCut_2.20\mp3DirectCut.exe"
  4. for /f "tokens=1,2*" %%a in ('dir /b *.mp3^|%JS% "%~0"') do (
  5.         set "Name=%%c"
  6.         set /a h1=%%a/3600,m1=%%a%%3600/60,s1=%%a%%3600%%60
  7.         set /a h2=%%b/3600,m2=%%b%%3600/60,s2=%%b%%3600%%60
  8.         set /a h1+=100,m1+=100,s1+=100,h2+=100,m2+=100,s2+=100
  9.         setlocal enabledelayedexpansion
  10.         rem set "t1=!h1:~1!:!m1:~1!:!s1:~1!"
  11.         rem set "t2=!h2:~1!:!m2:~1!:!s2:~1!"
  12.         set "t1=!m1:~1!:!s1:~1!"
  13.         set "t2=!m2:~1!:!s2:~1!"
  14.         echo,!t1! !t2! !Name!
  15.         endlocal
  16. )
  17. pause & exit/b & rem */
  18. WMP = new ActiveXObject("WMPlayer.OCX");
  19. s = "";
  20. while (!WScript.StdIn.AtEndOfStream) {
  21.         f = WScript.StdIn.ReadLine();
  22.         t = WMP.newMedia(f).getItemInfo("Duration");
  23.         t1 = Math.round(t/4);
  24.         t2 = Math.round(t*3/4);
  25.         s += t1 + " " + t2 + " " + f + "\n";
  26. }
  27. WScript.StdOut.Write(s);
复制代码
  1. @echo off
  2. set "mp3DirectCut=C:\Program Files (x86)\mp3DirectCut_2.20\mp3DirectCut.exe"  
  3. for %%f in (*.mp3) do (
  4.     echo FILE "%%f"> sh.cue
  5.     echo TRACK 01 AUDIO>>sh.cue
  6.     echo INDEX 01 %1>>sh.cue
  7.     echo TRACK 02 AUDIO>>sh.cue
  8.     echo INDEX 01 %2>>sh.cue
  9.     "%mp3DirectCut%" sh.cue /split C:\Temp
  10.     del C:\Temp\*02.mp3
  11. )
  12. del sh.cue
复制代码
怎么让第一个代码调用第二个?里面都有For循环,怕怕....

不循环的话能正常运行的是:
  1. @echo off
  2. set "mp3DirectCut=C:\Program Files (x86)\mp3DirectCut_2.20\mp3DirectCut.exe"
  3.     echo FILE "C:\test.mp3"> sh.cue
  4.     echo TRACK 01 AUDIO>>sh.cue
  5.     echo INDEX 01 01:10>>sh.cue
  6.     echo TRACK 02 AUDIO>>sh.cue
  7.     echo INDEX 01 03:30>>sh.cue
  8.     "%mp3DirectCut%" sh.cue /split C:\Temp
  9.     del C:\Temp\*02.mp3
  10. del sh.cue
复制代码
你的t1 要替换掉01:10
t2 要替换掉03:30
而遍历的所有mp3文件要替换掉 "C:\test.mp3"

TOP

本帖最后由 pendave 于 2015-1-27 22:27 编辑

回复 3# apang
我刚才用2楼的代码获得了mp3的长度了
echo %_prop_长度%

比如这个值是 00:04:58

我想把它变成 04:58 就是格式是向左移位了的,分:秒

是不是改下你代码中的
  1. set "t1=!m1:~1!:!s1:~1!"
  2. set "t2=!m2:~1!:!s2:~1!"
复制代码

TOP

本帖最后由 pendave 于 2015-1-27 23:44 编辑

我刚才用2楼的代码获得了mp3的长度了
echo %_prop_长度%

TOP

  1. @set @n=0; /* & echo off
  2. pushd "F:\music\"
  3. set "JS=cscript -nologo -e:jscript"
  4. for /f "tokens=1,2*" %%a in ('dir /b *.mp3^|%JS% "%~0"') do (
  5.         set "Name=%%c"
  6.         set /a h1=%%a/3600,m1=%%a%%3600/60,s1=%%a%%3600%%60
  7.         set /a h2=%%b/3600,m2=%%b%%3600/60,s2=%%b%%3600%%60
  8.         set /a h1+=100,m1+=100,s1+=100,h2+=100,m2+=100,s2+=100
  9.         setlocal enabledelayedexpansion
  10.         set "t1=!h1:~1!:!m1:~1!:!s1:~1!"
  11.         set "t2=!h2:~1!:!m2:~1!:!s2:~1!"
  12.         echo,!t1! !t2! !Name!
  13.         endlocal
  14. )
  15. pause & exit/b & rem */
  16. WMP = new ActiveXObject("WMPlayer.OCX");
  17. s = "";
  18. while (!WScript.StdIn.AtEndOfStream) {
  19.         f = WScript.StdIn.ReadLine();
  20.         t = WMP.newMedia(f).getItemInfo("Duration");
  21.         t1 = Math.round(t/4)
  22.         t2 = Math.round(t*3/4)
  23.         s += t1 + " " + t2 + " " + f + "\n"
  24. }
  25. WScript.StdOut.Write(s)
复制代码
调用第3方自己搞定

TOP

这里看到 .bat .vbs 配合获取mp3信息的代码

http://www.experts-exchange.com/ ... DOS/Q_27077456.html

Batch File Read File Attributes advanced.bat
  1. @echo off
  2. setlocal
  3. set FileName=C:\test.mp3
  4. for /F "tokens=1* delims==" %%A in ('cscript getinfo.vbs "%FileName%"') do set _prop_%%A=%%B
  5. set _prop
复制代码
getinfo.vbs
  1. if (WScript.Arguments.Count > 0) Then
  2.   strFile = WScript.Arguments(0)
  3. Else
  4.   WScript.Echo "ERROR : No input filename specified."
  5.   WScript.Quit -1
  6. End If
  7. Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
  8. If Not objFSO.FileExists(strFile) then
  9.   WScript.Echo "ERROR : Input filename specified does not exist."
  10.   WScript.Quit -1
  11. End If
  12. Set objFile = objFSO.GetFile(strFile)
  13. strFolder = objFile.ParentFolder
  14. Dim strAttrName(300)
  15. Set objShell = CreateObject("Shell.Application")
  16. Set objFolder = objShell.NameSpace(strFolder)
  17. for N = 0 To 300
  18.    strAttrName(N) = Replace(objFolder.GetDetailsOf(Nothing, N), " ", "_")
  19. Next
  20. Set objFileItem = objFolder.ParseName(objFile.Name)
  21. For i = 0 To 300
  22.    if objFolder.GetDetailsOf(objFileItem, i) <> "" Then
  23.       If Instr("objFolder.GetDetailsOf(objFileItem, i)", Chr(63)) then Wscript.Echo "FOUND"
  24.       WScript.Echo strAttrName(i) & "=" & Replace(objFolder.GetDetailsOf(objFileItem, i), Chr(63), "")
  25.    End If
  26. Next
  27. Wscript.Quit 0
复制代码
在BAT里怎么计算出一首mp3的1/4位置的时间点和 3/4位置的时间点呢?

TOP

返回列表