Write cover to FLAC file

Post Reply
jmmc
User
Posts: 8
Joined: Mon Jan 15, 2018 8:48 am
Are you a spambot?: no

Write cover to FLAC file

Post by jmmc » Mon Jan 15, 2018 9:01 am

I've only found information for MP3 files but it doesn't work with FLAC files.

I'm using this to add the image file to the flac but it doesn't work.

Code: Select all

$TagData['attached_picture'][0]['data'] = file_get_contents($albumcover);
$TagData['attached_picture'][0]['picturetypeid'] = 3; 
$TagData['attached_picture'][0]['description'] = "Cover";
$TagData['attached_picture'][0]['mime']  = 'image/jpeg';
This code only works with MP3 files.

Anyone knows how to fix that code for FLAC files?

Thank you in advanced.

James Heinrich
getID3() v1 developer
Posts: 1475
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: Write cover to FLAC file

Post by James Heinrich » Mon Jan 15, 2018 4:45 pm

"attached_picture" is only for ID3v2.
For a generalized solution you shouldn't look at specific tag data, but rely on CopyTagsToComments to give you unified data in the top-level [comments] key. This applies for any common tag data (title, artist, album, etc), as well as attached pictures.

This short example will list the attached pictures for any supported file format:

Code: Select all

require_once('getid3.php');
$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze($FullFileName);
getid3_lib::CopyTagsToComments($ThisFileInfo);
if (!empty($ThisFileInfo['comments']['picture'])) {
	echo 'Found '.count($ThisFileInfo['comments']['picture']).' pictures:<br>';
	foreach ($ThisFileInfo['comments']['picture'] as $key => $details) {
		echo '* $ThisFileInfo[comments][picture]['.$key.'] :: '.$details['picturetype'].' ('.$details['image_width'].'x'.$details['image_height'].','.$details['image_mime'].') = '.number_format($details['datalength']).' bytes<br>';
	}
}
I would also suggest making use of /demos/demo.browse.php (open in your browser and browse to the file in question) to see what data is returned where for any file.

jmmc
User
Posts: 8
Joined: Mon Jan 15, 2018 8:48 am
Are you a spambot?: no

Re: Write cover to FLAC file

Post by jmmc » Mon Jan 15, 2018 6:37 pm

James Heinrich wrote:
Mon Jan 15, 2018 4:45 pm
"attached_picture" is only for ID3v2.
For a generalized solution you shouldn't look at specific tag data, but rely on CopyTagsToComments to give you unified data in the top-level [comments] key. This applies for any common tag data (title, artist, album, etc), as well as attached pictures.

This short example will list the attached pictures for any supported file format:

Code: Select all

require_once('getid3.php');
$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze($FullFileName);
getid3_lib::CopyTagsToComments($ThisFileInfo);
if (!empty($ThisFileInfo['comments']['picture'])) {
	echo 'Found '.count($ThisFileInfo['comments']['picture']).' pictures:<br>';
	foreach ($ThisFileInfo['comments']['picture'] as $key => $details) {
		echo '* $ThisFileInfo[comments][picture]['.$key.'] :: '.$details['picturetype'].' ('.$details['image_width'].'x'.$details['image_height'].','.$details['image_mime'].') = '.number_format($details['datalength']).' bytes<br>';
	}
}
I would also suggest making use of /demos/demo.browse.php (open in your browser and browse to the file in question) to see what data is returned where for any file.
But in that code you don't inject a .jpg to a FLAC file. I want to do that, not read the pic from a FLAC file.

Is there any way to inject the jpg?

Thanks in advanced.

James Heinrich
getID3() v1 developer
Posts: 1475
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: Write cover to FLAC file

Post by James Heinrich » Mon Jan 15, 2018 8:09 pm

Oh, I misunderstood you, I though you were looking to read images, not write them, sorry.

I have made some changes to support writing attached pictures with metaflac:
https://github.com/JamesHeinrich/getID3 ... 339f2e8593

This type of code should now write a picture to your FLAC file:

Code: Select all

require_once('getid3/getid3.php');
require_once('getid3/write.php');
$tagwriter = new getid3_writetags;
$tagwriter->filename = '/path/to/test.flac';
$tagwriter->tagformats = array('metaflac');
$TagData['attached_picture'][0]['data'] = file_get_contents('/path/to/picture.jpg');
$TagData['attached_picture'][0]['description'] = 'picture description';
//$TagData['attached_picture'][0]['picturetypeid'] = 3; //optional, defaults to "Cover (front)" [3]
//$TagData['attached_picture'][0]['mime'] = 'image/jpeg'; // optional, should be detected automatically
$tagwriter->tag_data = $TagData;
$tagwriter->WriteTags();
if (!empty($tagwriter->errors) {
	echo 'Error: ';
	var_dump($tagwriter->errors);
}

jmmc
User
Posts: 8
Joined: Mon Jan 15, 2018 8:48 am
Are you a spambot?: no

Re: Write cover to FLAC file

Post by jmmc » Tue Jan 16, 2018 9:51 am

James Heinrich wrote:
Mon Jan 15, 2018 8:09 pm
Oh, I misunderstood you, I though you were looking to read images, not write them, sorry.

I have made some changes to support writing attached pictures with metaflac:
https://github.com/JamesHeinrich/getID3 ... 339f2e8593

This type of code should now write a picture to your FLAC file:

Code: Select all

require_once('getid3/getid3.php');
require_once('getid3/write.php');
$tagwriter = new getid3_writetags;
$tagwriter->filename = '/path/to/test.flac';
$tagwriter->tagformats = array('metaflac');
$TagData['attached_picture'][0]['data'] = file_get_contents('/path/to/picture.jpg');
$TagData['attached_picture'][0]['description'] = 'picture description';
//$TagData['attached_picture'][0]['picturetypeid'] = 3; //optional, defaults to "Cover (front)" [3]
//$TagData['attached_picture'][0]['mime'] = 'image/jpeg'; // optional, should be detected automatically
$tagwriter->tag_data = $TagData;
$tagwriter->WriteTags();
if (!empty($tagwriter->errors) {
	echo 'Error: ';
	var_dump($tagwriter->errors);
}
Now, It tries to write the picture but I get the following error:

Code: Select all

System call to metaflac failed with this message returned: 

C:\xampp\htdocs\functions\getid3\..\helperapps\metaflac.exe: unrecognized option `--import-picture-from=3|image/jpeg|Cover||C:\xampp\tmp\getCB1D.tmp'
==============================================================================
metaflac - Command-line FLAC metadata editor version 1.1.2
Copyright (C) 2001,2002,2003,2004,2005  Josh Coalson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
==============================================================================

This is the short help; for full help use 'metaflac --help'

Usage:
  metaflac [options] [operations] FLACfile [FLACfile ...]

Use metaflac to list, add, remove, or edit metadata in one or more FLAC files.
You may perform one major operation, or many shorthand operations at a time.

Options:
--preserve-modtime    Preserve the original modification time in spite of edits
--with-filename       Prefix each output line with the FLAC file name
                      (the default if more than one FLAC file is specified)
--no-filename         Do not prefix each output line with the FLAC file name
                      (the default if only one FLAC file is specified)
--no-utf8-convert     Do not convert tags from UTF-8 to local charset,
                      or vice versa.  This is useful for scripts.
--dont-use-padding    By default metaflac tries to use padding where possible
                      to avoid rewriting the entire file if the metadata size
                      changes.  Use this option to tell metaflac to not take
                      advantage of padding this way.
Should I update metaflac.exe or something?

And another thing, you forgot to delete an echo in write.metaflac.php line 101.

Thanks in advanced.

jmmc
User
Posts: 8
Joined: Mon Jan 15, 2018 8:48 am
Are you a spambot?: no

Re: Write cover to FLAC file

Post by jmmc » Tue Jan 16, 2018 9:58 am

I've updated metaflac and now it works!

Thanks for all :)

James Heinrich
getID3() v1 developer
Posts: 1475
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: Write cover to FLAC file

Post by James Heinrich » Tue Jan 16, 2018 2:43 pm

jmmc wrote:
Tue Jan 16, 2018 9:51 am
Should I update metaflac.exe or something?
And another thing, you forgot to delete an echo in write.metaflac.php line 101.
Yes, I'm not sure which version of metaflac introduced the picture options, but it's newer than the previously-included v1.1.2 from 2005. I already updated metaflac.exe in https://github.com/JamesHeinrich/getID3 ... fca2609603

And I have removed the debug info in https://github.com/JamesHeinrich/getID3 ... 96a198f992

jmmc
User
Posts: 8
Joined: Mon Jan 15, 2018 8:48 am
Are you a spambot?: no

Re: Write cover to FLAC file

Post by jmmc » Wed Jan 17, 2018 8:52 am

James Heinrich wrote:
Tue Jan 16, 2018 2:43 pm
jmmc wrote:
Tue Jan 16, 2018 9:51 am
Should I update metaflac.exe or something?
And another thing, you forgot to delete an echo in write.metaflac.php line 101.
Yes, I'm not sure which version of metaflac introduced the picture options, but it's newer than the previously-included v1.1.2 from 2005. I already updated metaflac.exe in https://github.com/JamesHeinrich/getID3 ... fca2609603

And I have removed the debug info in https://github.com/JamesHeinrich/getID3 ... 96a198f992
Perfect! Thanks for your help! :)

jmmc
User
Posts: 8
Joined: Mon Jan 15, 2018 8:48 am
Are you a spambot?: no

Re: Write cover to FLAC file

Post by jmmc » Wed Jan 17, 2018 12:25 pm

I've realized when I write more than one artist, in iTunes it shows only the last one. Is that fixable?

Thanks again!

James Heinrich
getID3() v1 developer
Posts: 1475
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: Write cover to FLAC file

Post by James Heinrich » Wed Jan 17, 2018 4:08 pm

Are the multiple artists being correctly written? (i.e. check with another program that will show what's in the file, like /demo/demo.browse.php in your browser)
If so, then iTunes is poorly designed/broken (wouldn't be the first time, it has a long history of mishandling metadata) and there's not much you can do. You can either write the multiple artists correctly and have iTunes not display it right, or merge the multiple artists yourself and write a single artist tag: ("Elvis; Beethoven") instead of ("Elvis", "Beethoven").

jmmc
User
Posts: 8
Joined: Mon Jan 15, 2018 8:48 am
Are you a spambot?: no

Re: Write cover to FLAC file

Post by jmmc » Fri Jan 19, 2018 10:03 am

James Heinrich wrote:
Wed Jan 17, 2018 4:08 pm
Are the multiple artists being correctly written? (i.e. check with another program that will show what's in the file, like /demo/demo.browse.php in your browser)
If so, then iTunes is poorly designed/broken (wouldn't be the first time, it has a long history of mishandling metadata) and there's not much you can do. You can either write the multiple artists correctly and have iTunes not display it right, or merge the multiple artists yourself and write a single artist tag: ("Elvis; Beethoven") instead of ("Elvis", "Beethoven").
Yes, it's itunes problem.

Thanks for all! :D

jmmc
User
Posts: 8
Joined: Mon Jan 15, 2018 8:48 am
Are you a spambot?: no

Re: Write cover to FLAC file

Post by jmmc » Wed Jan 24, 2018 1:07 pm

There is another error in the write.metaflac and it's that on *nix systems you forgot to add picture inject.

I fixed it by copying it from Windows method.

Thanks!

James Heinrich
getID3() v1 developer
Posts: 1475
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: Write cover to FLAC file

Post by James Heinrich » Wed Jan 24, 2018 1:41 pm


Post Reply