首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按歌曲持续时间将文件拆分到子文件夹中

按歌曲持续时间将文件拆分到子文件夹中
EN

Stack Overflow用户
提问于 2017-10-08 16:01:27
回答 1查看 123关注 0票数 0

我对PowerShell完全陌生,通过搜索它,我没有发现任何关于它的东西。所以我会在这里询问是否有人知道这是否可能,以及如何做到.

我在一个文件夹中有1000个mp3文件。我想把它们分成子文件夹,但是每个子文件夹的播放时间应该是1小时,或者至少尽可能接近1小时(最好大于1h,而不是更短)。

因此,我必须查看mp3文件的长度(通常是2-4分钟)。我想首先获得足够的mp3文件,这样总共就有1小时的播放时间了。然后将它们移到子文件夹中,并将其命名为List *n*,其中n对每个子文件夹递增。类似:列表1,列表2,列表3

这个是可能的吗?

EN

回答 1

Stack Overflow用户

发布于 2017-10-08 18:01:06

更新:这是工作脚本!编辑播放列表路径(主目录,将创建所有子文件夹)。同时编辑播放列表的长度!

代码语言:javascript
复制
Function Get-MP3Data
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([Psobject])]
    Param
    (
        [String] [Parameter(Mandatory=$true, ValueFromPipeline=$true)] $Directory
    )

    Begin
    {
        $shell = New-Object -ComObject "Shell.Application"
    }
    Process
    {

        Foreach($Dir in $Directory)
        {
            $ObjDir = $shell.NameSpace($Dir)
            $Files = gci $Dir| ?{$_.Extension -in '.mp3','.mp4'}

            Foreach($File in $Files)
            {
                $ObjFile = $ObjDir.parsename($File.Name)
                $MetaData = @{}
                $MP3 = ($ObjDir.Items()|?{$_.path -like "*.mp3" -or $_.path -like "*.mp4"})
                $PropertArray = 0,1,2,12,13,14,15,16,17,18,19,20,21,22,27,28,36,220,223

                Foreach($item in $PropertArray)
                { 
                    If($ObjDir.GetDetailsOf($ObjFile, $item)) #To avoid empty values
                    {
                        $MetaData[$($ObjDir.GetDetailsOf($MP3,$item))] = $ObjDir.GetDetailsOf($ObjFile, $item)
                    }

                }

                New-Object psobject -Property $MetaData |select *, @{n="Directory";e={$Dir}}, @{n="Fullname";e={Join-Path $Dir $File.Name -Resolve}}, @{n="Extension";e={$File.Extension}}
            }
        }
    }
    End
    {
    }
}

# Create playlist
$TotalLength = 0
$MaxLength = 3600
$TempPlaylist = @()
$PlaylistName = 1
$PlaylistPath = "C:\Users\Admin\Desktop\New folder"

if ($TotalLength -lt $MaxLength)
{   
    ForEach($item in ($PlaylistPath |Get-Mp3Data)){

        # Get all song names and song durations
        $SongName = $item.Fullname
        $Seconds = [int](([datetime]$item.Length).TimeOfDay.TotalSeconds)

        # Append song duration to total length and add song to temporary playlist
        $TotalLength += $Seconds
        $TempPlaylist += ,$SongName

        # Create a folder for the playlist if it doesn't exist
        if (!(test-path $PlaylistPath\$PlaylistName))
        {
            New-item -ItemType Directory -Force -Name $PlaylistName -Path $PlaylistPath
            Write-Host Created new folder $PlaylistName
        }

        # Check if the total length is >= maxlength, then move temporary playlist to a new folder
        if ($TotalLength -ge $MaxLength)
        {   
            # Loop through all songs in the temporary playlist
            For($i=0; $i -lt $TempPlaylist.length; $i++)
            {
                Move-Item -Path $TempPlaylist[$i] -Destination $PlaylistName
                Write-Host Moved file $TempPlaylist[$i] to $PlaylistName
            }

            # Increment playlist folder name
            $PlaylistName++

            # Reset the temporary playlist
            $TempPlaylist = @()

            # Reset the temporary playlist length
            $TotalLength = 0
        }
    }
}

若要向其添加随机化(以便在插入播放列表之前对mp3文件进行随机排序),只需更改即可。

代码语言:javascript
复制
ForEach($item in ($PlaylistPath | Get-Mp3Data)){

代码语言:javascript
复制
ForEach($item in ($PlaylistPath | Get-Mp3Data) | Sort-Object {Get-Random}){
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46633136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档