[Solved] How to write 3/4 data of a file to another empty file?
-
Hello!
I have an empty file called fileout.ogg, and a file (filein.ogg) that have 100 seconds of sound.
So, how can I write into the empty file just the first 75 seconds of filein.ogg with Qt's Classes?
Thanks for any reply!
[Below Solved]
BTW, a little off-topic. I have coded some line to copy files from a directory to another directory, it's like:@ QDir source("original");
QStringList filesList = directory.entryList(QDir::Files);
QString fileName;
foreach(fileName, fileList){
QFile sourceFile (fileName);
sourceFile.copy (Path/to/tartget/directory/ + fileName);
}
@
It seem I have done something wrong with this, if anyone can point it out, thanks a million! -
Hmm, I ussually work with QFileInfoList for such operations.
It's just a guess but I think QDir::entryList(..) only returns the filenames and not their complete paths, and that may be the reason why QFile sourceFile doesn't work and thus sourceFile.copy(..) also doesn't work.At first I would suggest you check the return value of QFile::copy()
@
if(sourceFile.copy(Path/to/tartget/directory/ + fileName))
{
// success
}
else
{
// failed
}
@If this always fails then maybe using QDir::entryInfoList(..) and QFileInfo::absoluteFilePath(..)
will get you the result you're looking for. -
For the audio, you need to:
Read raw bytes from filein.ogg
Decode raw bytes (convert them into audio samples)
Copy 3/4 of the audio samples
Encode the copied samples (convert them into raw bytes)
Write raw bytes into fileout.ogg
There's no Qt function for that... you need a 3rd-party library for decoding/encoding
-
Thanks alot, KA51O! Below are my codes for those interested:
@
QDir sourcePath("source");
QStringList filesList = sourcePath.entryList(QDir::Files);
QString fileName;
foreach(fileName, fileList){
QFile sourceFile (QDir().toNativeSeperators (sourcePath.absolutePath() + QString ("/") + fileName));
sourceFile.copy (QDir().toNativeSeperators (QString ("path/to/tartget/directory/") + fileName);
}@
@JKSH: I thought Qt should have some funtion which help me analize a file's size, then read till it reach the 3/4 point?
-
You can't just take 3/4 of coded file and save it in another...
This will work with text or raw file formats, but not with extended formats like music/video/archive/documents etc... They all have strict structure of content, with header where the extended information about content/codec name/lenght/quality/compression method, etc is saved.....If you want 100sec of ogg file, you need to read file header, get all info about encoding and codecs, when read so called "pages" of ogg, it's usually 4-8kb, they also have header with information how to decompress this page, crc summ, sequence number etc...
If you like to copy some seconds of ogg file into another one, you should use libogg for that...
Ogg Encapsulation Format "RFC3533":http://www.ietf.org/rfc/rfc3533.txt
"Reading Ogg files using libogg":http://www.bluishcoder.co.nz/2009/06/24/reading-ogg-files-using-libogg.html -
[quote author="A.A.B.A" date="1346148193"]I thought Qt should have some funtion which help me analize a file's size, then read till it reach the 3/4 point?[/quote]Qt can:
- Analyze file size -- QFile::size()
- Read raw data -- QFile::read()
Qt CANNOT:
- Decode/Encode .ogg data
Why not? Because hundreds and thousands of file types exist in the world. Qt developers don't have time/energy to write encoders/decoders for all of them.
An .ogg file contains sound data, mixed with OTHER data. Read AcerExtensa's link for details. If you just chop off the last 1/4 of your file, it will become corrupted.
-
Wow, I didn't know working with other file types could be so complicated.
Thanks so much for both of your's info, JKSH and AcerExtensa! I'm really apreciated!
-
[quote author="A.A.B.A" date="1346155719"]Wow, I didn't know working with other file types could be so complicated.
Thanks so much for both of your's info, JKSH and AcerExtensa! I'm really apreciated![/quote]Yea, they're a a little complicated... but that makes them more powerful :)
You're welcome!
-
A.B.B.A.: This is not necessary:
@QFile sourceFile (QDir().toNativeSeperators (sourcePath.absolutePath() + QString ("/") + fileName));@QFile wants a string in with / as separators, just like any other file related method in Qt. So there is no need to use toNativeSeparators here! That only ever makes sense when presenting pathes to the user.
-
[quote author="Tobias Hunger" date="1346231933"]A.B.B.A.: This is not necessary:
@QFile sourceFile (QDir().toNativeSeperators (sourcePath.absolutePath() + QString ("/") + fileName));@QFile wants a string in with / as separators, just like any other file related method in Qt. So there is no need to use toNativeSeparators here! That only ever makes sense when presenting pathes to the user.[/quote]
Hm, I didn't know it.
Thanks for your clearance, Tobias!