1

感谢您花点时间阅读本文,此时我正在尝试使用 icecast 完成和润色一个小广播电台,实际上一切都已经开始工作了。

但是有一个名为 ezstream 的程序,它只是在没有人直播的时候将音乐流式传输到 icecast,它是最常见的 autodj,它使用播放列表。

说播放列表可以播放一次,然后程序关闭,这正是我想在这里利用的,我设法自动创建了两个不同的播放列表,一个包含所有音乐,另一个包含广告、叮当声和声音字节,基本上它们是只是存储在计算机上的文件列表。像下面这样,显然他们有名字和东西。

这将是播放列表1

/home/mp3/albums/album1/title1.mp3

/home/mp3/albums/album1/title2.mp3

/home/mp3/albums/album1/title3.mp3

/home/mp3/albums/album1/title4.mp3

/home/mp3/albums/album1/title5.mp3

/home/mp3/albums/album2/title1.mp3

/home/mp3/albums/album2/title2.mp3

/home/mp3/albums/album2/title3.mp3

/home/mp3/albums/album2/title4.mp3

/home/mp3/albums/album2/title5.mp3

playlist2 非常相似,但它只包含广告,所以看起来像这样

/home/mp3/commercials/commercial1.mp3

/home/mp3/commercials/commercial2.mp3

/home/mp3/commercials/commercial3.mp3

/home/mp3/commercials/commercial4.mp3

/home/mp3/commercials/commercial5.mp3

我严重卡住的部分是以 2:1 或 3:1 的比例合并那些(如果你能帮我处理这两个代码,那也很棒。

最终输出应该看起来像这样

/home/mp3/albums/album1/title1.mp3

/home/mp3/albums/album1/title2.mp3

/home/mp3/commercials/commercial1.mp3

/home/mp3/albums/album1/title3.mp3

/home/mp3/albums/album1/title4.mp3

/home/mp3/commercials/commercial2.mp3

/home/mp3/albums/album1/title5.mp3

/home/mp3/albums/album2/title1.mp3

/home/mp3/commercials/commercial2.mp3

依此类推,直到两个文件完全合并,到目前为止,我只设法找到此代码,但由于某种原因它不起作用,它给出了与语法相关的错误并缺少 < 符号。

AWK 代码:

awk ‘FNR==NR{

song[FNR]=$0; 
next 
}

{

print song[FNR+line];line++;

print song[FNR+line]

print $0

}’ playlist1.m3u playlist2.m3u

所有这些都应该输出到第三个文件说mergedplaylists.m3u

我没有创建该代码,尽管我已经尝试摆弄了一段时间,但我不太清楚的一件事是为什么它在那里说“歌曲”,可以将其更改为其他内容吗?说“专辑”?

该代码根本不起作用,也没有在输出文件中写入任何内容(我也不知道它是否正确)。

我希望 some1 可以帮助我处理特定情况,AWK 似乎很有帮助,但它非常神秘,我发现很多问题来理解它......

再次感谢你

4

1 回答 1

1

这将在每个广告之前放置 2 首歌曲:

awk '
FNR==NR{ song[++numSongs]=$0; next }
{
   for (i=1;i<=2;i++)
      print song[++songNr]
   print
}
songNr == numSongs { exit }
' playlist1.m3u playlist2.m3u

将“2”更改为“3”或您认为合适的任何内容。

基于以下评论的替代实施:

$ cat tst.awk
BEGIN{ interval = (interval ? interval : 3) }

NR==FNR { songs[++numSongs] = $0; next }

{ commercials[++numCommercials] = $0 }

END {
    for (songNr=1; songNr<=numSongs; songNr++) {

        print songs[songNr]

        if ( !( songNr % interval) && (++commercialNr in commercials) )
            print commercials[commercialNr]

    }

}
$
$ cat songs.txt
/home/mp3/albums/album1/title1.mp3
/home/mp3/albums/album1/title2.mp3
/home/mp3/albums/album1/title3.mp3
/home/mp3/albums/album1/title4.mp3
/home/mp3/albums/album1/title5.mp3
/home/mp3/albums/album2/title1.mp3
/home/mp3/albums/album2/title2.mp3
/home/mp3/albums/album2/title3.mp3
/home/mp3/albums/album2/title4.mp3
/home/mp3/albums/album2/title5.mp3
$
$ cat commercials.txt
/home/mp3/commercials/commercial1.mp3
/home/mp3/commercials/commercial2.mp3
/home/mp3/commercials/commercial3.mp3
/home/mp3/commercials/commercial4.mp3
/home/mp3/commercials/commercial5.mp3
$
$ awk -f tst.awk songs.txt commercials.txt
/home/mp3/albums/album1/title1.mp3
/home/mp3/albums/album1/title2.mp3
/home/mp3/albums/album1/title3.mp3
/home/mp3/commercials/commercial1.mp3
/home/mp3/albums/album1/title4.mp3
/home/mp3/albums/album1/title5.mp3
/home/mp3/albums/album2/title1.mp3
/home/mp3/commercials/commercial2.mp3
/home/mp3/albums/album2/title2.mp3
/home/mp3/albums/album2/title3.mp3
/home/mp3/albums/album2/title4.mp3
/home/mp3/commercials/commercial3.mp3
/home/mp3/albums/album2/title5.mp3
$
$ awk -v interval=1 -f tst.awk songs.txt commercials.txt
/home/mp3/albums/album1/title1.mp3
/home/mp3/commercials/commercial1.mp3
/home/mp3/albums/album1/title2.mp3
/home/mp3/commercials/commercial2.mp3
/home/mp3/albums/album1/title3.mp3
/home/mp3/commercials/commercial3.mp3
/home/mp3/albums/album1/title4.mp3
/home/mp3/commercials/commercial4.mp3
/home/mp3/albums/album1/title5.mp3
/home/mp3/commercials/commercial5.mp3
/home/mp3/albums/album2/title1.mp3
/home/mp3/albums/album2/title2.mp3
/home/mp3/albums/album2/title3.mp3
/home/mp3/albums/album2/title4.mp3
/home/mp3/albums/album2/title5.mp3
$
$ awk -v interval=2 -f tst.awk songs.txt commercials.txt
/home/mp3/albums/album1/title1.mp3
/home/mp3/albums/album1/title2.mp3
/home/mp3/commercials/commercial1.mp3
/home/mp3/albums/album1/title3.mp3
/home/mp3/albums/album1/title4.mp3
/home/mp3/commercials/commercial2.mp3
/home/mp3/albums/album1/title5.mp3
/home/mp3/albums/album2/title1.mp3
/home/mp3/commercials/commercial3.mp3
/home/mp3/albums/album2/title2.mp3
/home/mp3/albums/album2/title3.mp3
/home/mp3/commercials/commercial4.mp3
/home/mp3/albums/album2/title4.mp3
/home/mp3/albums/album2/title5.mp3
/home/mp3/commercials/commercial5.mp3
$
$ awk -v interval=4 -f tst.awk songs.txt commercials.txt
/home/mp3/albums/album1/title1.mp3
/home/mp3/albums/album1/title2.mp3
/home/mp3/albums/album1/title3.mp3
/home/mp3/albums/album1/title4.mp3
/home/mp3/commercials/commercial1.mp3
/home/mp3/albums/album1/title5.mp3
/home/mp3/albums/album2/title1.mp3
/home/mp3/albums/album2/title2.mp3
/home/mp3/albums/album2/title3.mp3
/home/mp3/commercials/commercial2.mp3
/home/mp3/albums/album2/title4.mp3
/home/mp3/albums/album2/title5.mp3

这是awk:

BEGIN{ interval = (interval ? interval : 3) }

NR==FNR { songs[++numSongs] = $0; next }

{ commercials[++numCommercials] = $0 }

END {
    for (songNr=1; songNr<=numSongs; songNr++) {

        print songs[songNr]

        if ( !(songNr % interval) && (++commercialNr <= numCommercials) )
            print commercials[commercialNr]

    }

}

这是类似 C 的伪代码:

void main() {

    FILE *filep;
    char *line;

    char *songs[1000];
    char *commercials[1000];

    int FNR = 0;
    int NR = 0;
    int interval = 0;
    int numSongs = 0;
    int numCommercials = 0;
    int songNr = 0;
    int commercialNr = 0;
    int argNr = 0;

    /* BEGIN */
    if (ARGV[++argNr] == "interval") {
         interval = ARGV[++argNr];
    }
    interval = (interval ? interval : 3);

    for (++argNr;argNr<=ARGC;argNr++) {
       filep = ARGV[argNr];
       FNR = 0;
       while ( fgets(line,filep) > 0 ) {
          NR++;
          FNR++;

          if (NR == FNR) { songs[++numSongs] = line; continue; }

          commercials[++numCommercials] = line;

       }
    }

    /* END */
    for (songNr=1; songNr<=numSongs; songNr++) {

        printf("%s\n",songs[songNr]);

        if ( !(songNr % interval) && (++commercialNr <= numCommercials) )
                printf("%s\n",commercials[commercialNr]);
        }

    }

    return;
}

为了便于比较,我对 awk 脚本所做的唯一更改是删除了没有明确的 C 等效项的“in”运算符。

希望这有助于澄清 awk 脚本在做什么。

于 2013-04-05T18:07:00.823 回答