Haskell 操作文件内容

发布于 2024-12-21 10:21:29 字数 2734 浏览 2 评论 0原文

我是一名来自葡萄牙的学生,对我必须处理的项目有一些疑问。

我的最终目标是使用 LaTeX 创建一个 pdf 目录,使用 exiftool 存储有关文件的信息。

到目前为止,我已经设法将音频从视频文件中分离出来,并将其经过 exiftool 处理的信息存储在一个文件中,但它会使它们变大。

例如:

======== Cartoon Battle.mp3
-ExifToolVersion=8.60
-FileName=Cartoon Battle.mp3
-Directory=.
-FileSize=4.0 MB
-FileModifyDate=2011:12:13 09:46:25+00:00
-FilePermissions=rw-rw-r--
-FileType=MP3
-MIMEType=audio/mpeg
-MPEGAudioVersion=1
-AudioLayer=3
-AudioBitrate=320 kbps
-SampleRate=48000
-ChannelMode=Stereo
-MSStereo=Off
-IntensityStereo=Off
-CopyrightFlag=False
-OriginalMedia=False
-Emphasis=None
-ID3Size=113441
-Title=Cartoon Battle
-Artist=Kevin MacLeod
-Year=2007
-BeatsPerMinute=130
-Genre=Unclassifiable
-Comment=(iTunPGAP) 0
-EncodedBy=iTunes v7.0.2.16
-Comment=(iTunNORM)  000001F7 0000014B 00001DBD 00000B18 000154C8 00000780 00008169                 00008180 00000780 00000780
-Comment=(iTunSMPB)  00000000 00000210 00000A84 00000000004A606C 00000000 003DE780 00000000 00000000 00000000 00000000 00000000 00000000
-Album=Far East
-Composer=Kevin MacLeod
-PictureFormat=JPG
-PictureType=Other
-PictureDescription=
-Picture=(Binary data 91855 bytes, use -b option to extract)
-DateTimeOriginal=2007
-Duration=0:01:41 (approx)
======== Comic Plodding.mp3
-ExifToolVersion=8.60
-FileName=Comic Plodding.mp3
-Directory=.
-FileSize=3.8 MB
-FileModifyDate=2011:12:13 09:46:24+00:00
-FilePermissions=rw-rw-r--
-FileType=MP3
-MIMEType=audio/mpeg
-MPEGAudioVersion=1
-AudioLayer=3
-AudioBitrate=320 kbps
-SampleRate=44100
-ChannelMode=Joint Stereo
-MSStereo=Off
-IntensityStereo=Off
-CopyrightFlag=False
-OriginalMedia=False
-Emphasis=None
-ID3Size=105099
-EncoderSettings=Logic Pro 8.0.1
-Comment=(iTunNORM)  000001AE 00000181 000026DF 0000365B 0001100A 00016CE5 00007D33     00007ECF 00010FF0 00016CE5
-Comment=(iTunSMPB)  00000000 00000210 000009D6 000000000040DA1A 00000000 003ABCBC     00000000 00000000 00000000 00000000 00000000 00000000
-Artist=Kevin MacLeod
-Composer=Kevin MacLeod
-Year=2008
-Genre=Silent Film Score
-PictureFormat=JPG
-PictureType=Other
-PictureDescription=
-Picture=(Binary data 84880 bytes, use -b option to extract)
-Album=Scoring - Silent Film: Dark
-DateTimeOriginal=2008
-Duration=0:01:36 (approx)

我想做的是: 首先,尝试将该文件中的两首歌曲拆分为某种列表。 然后,尝试获取文件内的一些信息,例如文件名、大小等等。

到目前为止,我已经想出了这段代码,但它不正确:

mymain = do{
  a <- readFile "audio.txt" ; -- file that has all the infos collected by exiftool
  ml <- splitRegex (mkRegex "========") a ; -- I expect this to separate each song and place their corresponding information on a single string

任何人都可以给我提示吗?我想在我创建的文件结构上存储一些信息,但首先,我需要按歌曲将其拆分,然后选择我想要的,对吧?

感谢您的帮助,请原谅我的法语不好!

PS:我不太习惯haskell(刚刚开始)

I'm a student from Portugal having some doubts about a project I have to handle.

My final objective is to create a pdf catalog with LaTeX that stores information about files using exiftool.

So far, i've managed to separate audio from video files and store their exiftool'ed information on a file, but it bulks them up.

for example:

======== Cartoon Battle.mp3
-ExifToolVersion=8.60
-FileName=Cartoon Battle.mp3
-Directory=.
-FileSize=4.0 MB
-FileModifyDate=2011:12:13 09:46:25+00:00
-FilePermissions=rw-rw-r--
-FileType=MP3
-MIMEType=audio/mpeg
-MPEGAudioVersion=1
-AudioLayer=3
-AudioBitrate=320 kbps
-SampleRate=48000
-ChannelMode=Stereo
-MSStereo=Off
-IntensityStereo=Off
-CopyrightFlag=False
-OriginalMedia=False
-Emphasis=None
-ID3Size=113441
-Title=Cartoon Battle
-Artist=Kevin MacLeod
-Year=2007
-BeatsPerMinute=130
-Genre=Unclassifiable
-Comment=(iTunPGAP) 0
-EncodedBy=iTunes v7.0.2.16
-Comment=(iTunNORM)  000001F7 0000014B 00001DBD 00000B18 000154C8 00000780 00008169                 00008180 00000780 00000780
-Comment=(iTunSMPB)  00000000 00000210 00000A84 00000000004A606C 00000000 003DE780 00000000 00000000 00000000 00000000 00000000 00000000
-Album=Far East
-Composer=Kevin MacLeod
-PictureFormat=JPG
-PictureType=Other
-PictureDescription=
-Picture=(Binary data 91855 bytes, use -b option to extract)
-DateTimeOriginal=2007
-Duration=0:01:41 (approx)
======== Comic Plodding.mp3
-ExifToolVersion=8.60
-FileName=Comic Plodding.mp3
-Directory=.
-FileSize=3.8 MB
-FileModifyDate=2011:12:13 09:46:24+00:00
-FilePermissions=rw-rw-r--
-FileType=MP3
-MIMEType=audio/mpeg
-MPEGAudioVersion=1
-AudioLayer=3
-AudioBitrate=320 kbps
-SampleRate=44100
-ChannelMode=Joint Stereo
-MSStereo=Off
-IntensityStereo=Off
-CopyrightFlag=False
-OriginalMedia=False
-Emphasis=None
-ID3Size=105099
-EncoderSettings=Logic Pro 8.0.1
-Comment=(iTunNORM)  000001AE 00000181 000026DF 0000365B 0001100A 00016CE5 00007D33     00007ECF 00010FF0 00016CE5
-Comment=(iTunSMPB)  00000000 00000210 000009D6 000000000040DA1A 00000000 003ABCBC     00000000 00000000 00000000 00000000 00000000 00000000
-Artist=Kevin MacLeod
-Composer=Kevin MacLeod
-Year=2008
-Genre=Silent Film Score
-PictureFormat=JPG
-PictureType=Other
-PictureDescription=
-Picture=(Binary data 84880 bytes, use -b option to extract)
-Album=Scoring - Silent Film: Dark
-DateTimeOriginal=2008
-Duration=0:01:36 (approx)

What i'd like to do is :
first, try to split the two songs on that file in some kind of list of some sort.
then, try and pick up some of the information inside a file, like the FileName, Size and all that.

So far, i've come up with this piece of code, but it isnt correct:

mymain = do{
  a <- readFile "audio.txt" ; -- file that has all the infos collected by exiftool
  ml <- splitRegex (mkRegex "========") a ; -- I expect this to separate each song and place their corresponding information on a single string

Can anyone give me a hint? I want to store some information on a File structure i've created, but first, i need to split it up by songs, then pick up what I want, right?

THanks for the help and excuse me for my bad french!

PS: I'm not that used to haskell (just starting)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-12-28 10:21:29

最小的修复是:

import Text.Regex

main = do {
  a <- readFile "audio.txt" ;
  print $ splitRegex (mkRegex "========") a ;
}

箭头从单子值中提取值 - 从 m a 类型的值,其中 m 是单子,a 是任意类型。 readFile 返回一个单子值(IO String 类型),但 splitRegex 接受 String 类型的纯值。因此箭头可用于从IO String中提取String。但是 splitRegex 返回一个非单值值,因此 <- 无法从中提取任何内容。

我建议将您的代码拆分为 IO 代码和非 IO 代码,并使用不带 ;{} 的语法:

import Text.Regex

processData text = x where
  x = splitRegex (mkRegex "========") y
  y = text
  ...

main = do
  a <- readFile "audio.txt"
  print $ processData a

因此 IO 代码将使用 do<- 以及非 IO 代码将使用 where=

A minimal fix is:

import Text.Regex

main = do {
  a <- readFile "audio.txt" ;
  print $ splitRegex (mkRegex "========") a ;
}

The arrow extracts a value from a monadic value - from a value of type m a where m is a monad and a is an arbitrary type. readFile returns a monadic value (of type IO String) but splitRegex accepts a plain value of type String. So arrow can be used to extract a String from IO String. But splitRegex returns a non-monadic value so <- cannot extract anything from it.

I suggest to split your code into IO code and non-IO code and use the syntax without ; and {}:

import Text.Regex

processData text = x where
  x = splitRegex (mkRegex "========") y
  y = text
  ...

main = do
  a <- readFile "audio.txt"
  print $ processData a

So IO code will use do and <- and non-IO code will use where and =.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文