Rockbox.org home
Downloads
Release release
Dev builds dev builds
Extras extras
themes themes
Documentation
Manual manual
Wiki wiki
Device Status device status
Support
Forums forums
Mailing lists mailing lists
IRC IRC
Development
Bugs bugs
Patches patches
Dev Guide dev guide
translations translations
Search



Donate

Rockbox Technical Forums


Login with username, password and session length
Home Help Search Staff List Login Register
News:

Thank You for your continued support and contributions!

+  Rockbox Technical Forums
|-+  Support and General Use
| |-+  Audio Playback, Database and Playlists
| | |-+  Converting ID3 image tags from PNG to JPG
« previous next »
  • Print
Pages: [1]

Author Topic: Converting ID3 image tags from PNG to JPG  (Read 2333 times)

Offline wintermute23

  • Member
  • *
  • Posts: 122
  • What Would Batman Do?
Converting ID3 image tags from PNG to JPG
« on: April 13, 2024, 01:38:17 PM »
I have an issue which is similar to https://forums.rockbox.org/index.php/topic,54788.0.html.

I have a podcatcher I wrote myself in perl, which checks listed RSS feeds, looks for new items, downloads them and prepares an .m3u8 playlist for me. I have album art set for each feed in the appropriate directory, but quite often each episode has its own artwork embedded in PNG format, and I'd like to be able to convert it to JPG so that it shows up on RockBox.

Because this conversion happens when each file gets downloaded, batch processing isn't really a valid solution. I've got something that almost works, shown below. The JPG file it produces can be displayed by RockBox's Image Viewer plugin, and an ID3 inspector shows me that the finished MP3 file has a JPG embedded image. So every individual step seems to be working, but when I play the file, the album art isn't displayed.

Can anyone help me figure out what I might be missing? Thanks.

My image conversion code:
Code: [Select]
#!/usr/bin/perl

use strict;
use Imager;
use MP3::Tag;
use Data::Dumper;

my $input = "~/Documents/test.mp3";
my $mp3 = MP3::Tag->new($input);
my @tags = $mp3->get_id3v2_frame_ids();
print Dumper(@tags);
if ($mp3->have_id3v2_frame_by_descr("APIC")) {
    print "Picture exists\n";

    # Read the image into a variable
    my $png_img = $mp3->select_id3v2_frame_by_descr("APIC");

    # Import the image into Imager for conversion
    if (open(my $fh_png, "<:raw", \$png_img)){
        print "Is PNG\n";
        my $converter = Imager->new();
        $converter->read(fh=>$fh_png);

        $converter = $converter->scale(xpixels=>1000, ypixels=>1000, type=>'min');

        my $jpg_img;

        # Write to a stream that can be written into the ID# tag
        open(my $fh_jpg, ">:raw", \$jpg_img) or die "Cannot open JPG stream";
        $converter->write(fh=>$fh_jpg, type=>"jpeg", jpeg_progressive=>0) or die "Cannot write: ", $converter->errstr;
        close($fh_jpg);

        # Write to a file
        open(my $fh, ">:raw", "~/Documents/test.jpg") or die "Cannot open JPG file";
        $converter->write(fh=>$fh, type=>"jpeg", jpeg_progressive=>0) or die "Cannot write: ", $converter->errstr;
        close($fh);

        # Update the ID3 tag
        $mp3->select_id3v2_frame_by_descr("APIC", $jpg_img) or die "Cannot update MP3";
        $mp3->config(write_v24 => 1) or die "Cannot set config";
        $mp3->update_tags() or die "Cannot update tags";
        close($fh_png);
    }

}
Logged
Past: iRiver H320 w/ 32GB CF card mod; iPod 4th Gen 30GB; iPod 5th Gen 30GB; iPod Classic 80GB

Present: iPod Classic w/ 128GB SSD

Offline philden

  • Member
  • *
  • Posts: 126
Re: Converting ID3 image tags from PNG to JPG
« Reply #1 on: April 13, 2024, 02:45:00 PM »
I'm not experienced with Perl, but does your code generate a 1000x1000 pixel jpeg? If so this is very big. I'd try making a much smaller file which will take less memory to display. I'd suggest a maximum of 300x300, or use the exact size that your favourite theme uses.
Logged

Offline wintermute23

  • Member
  • *
  • Posts: 122
  • What Would Batman Do?
Re: Converting ID3 image tags from PNG to JPG
« Reply #2 on: April 13, 2024, 02:58:03 PM »
That's certainly worth trying. That 1000×1000 limit was added because the sample file I'm using had an embedded image that was 3600×3600, which seemed excessive. Quite a few of the music files I have have cover art in the 1000×1000 range, so it didn't occur to me it might be a problem.

And after a quick test, changing the image size to 190×190, it's still not working.
Logged
Past: iRiver H320 w/ 32GB CF card mod; iPod 4th Gen 30GB; iPod 5th Gen 30GB; iPod Classic 80GB

Present: iPod Classic w/ 128GB SSD

Offline philden

  • Member
  • *
  • Posts: 126
Re: Converting ID3 image tags from PNG to JPG
« Reply #3 on: April 13, 2024, 05:45:21 PM »
Have you tried saving the jpeg as a separate file instead of being embedded?
Logged

Offline wintermute23

  • Member
  • *
  • Posts: 122
  • What Would Batman Do?
Re: Converting ID3 image tags from PNG to JPG
« Reply #4 on: April 13, 2024, 06:16:08 PM »
Yeah, as I say, RockBox can read the file generated. I've considered just dropping the JPG into the directory with the same filename as the podcast (that should work, right?), but whit it copies podcasts over to my iPod, it reports a pretty naïve count of files copied, and I'd need to rework that to something more complicated to make it work properly.

Not entirely out of the question, but getting the art properly embedded is definitely the more elegant solution.
Logged
Past: iRiver H320 w/ 32GB CF card mod; iPod 4th Gen 30GB; iPod 5th Gen 30GB; iPod Classic 80GB

Present: iPod Classic w/ 128GB SSD

Offline saratoga

  • Developer
  • Member
  • *
  • Posts: 9369
Re: Converting ID3 image tags from PNG to JPG
« Reply #5 on: April 13, 2024, 09:12:31 PM »
One thing that jumps out at me is the write_v24 bit:
Quote
write_v24
If FALSE (default), writing of ID3v2.4 is prohibited (it is not fully supported; allow on your own risk).

If you try writing the default 2.3 tags do they parse?

-----

You could try decoding one of the files in the UI simulator to see if it throws an error.  1000x1000 is a bit big but should work fine. 

There are some old windows sim builds if you want to try quickly without building from scratch:  http://rasher.dk/rockbox/simulator/  They're really out of date but should be ok for testing ID3 parsing.

Logged

Offline wintermute23

  • Member
  • *
  • Posts: 122
  • What Would Batman Do?
Re: Converting ID3 image tags from PNG to JPG
« Reply #6 on: April 13, 2024, 09:37:38 PM »
The file I'm modifying has tags in v2.4 format; if I remove that config flag, it complains "Writing of ID3v2.4 is not fully supported (prohibited now via `write_v24')." and does not update the embedded image. It might be worth seeing if I can find a file with v2.3 tags, and playing around with that. Depending on how common v2.4 tags are, if that works it might be a decent solution.
Logged
Past: iRiver H320 w/ 32GB CF card mod; iPod 4th Gen 30GB; iPod 5th Gen 30GB; iPod Classic 80GB

Present: iPod Classic w/ 128GB SSD

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 1192
Re: Converting ID3 image tags from PNG to JPG
« Reply #7 on: April 14, 2024, 09:33:23 AM »
ID3 v 2.4 was just a bunch of new genre categories if I recall

 edit:
looks like 2.4 has a few other things to offer as well:
  • multi-value tags
  • date recorded and date released tags
  • alternate placement of id3 data in file
  • UTF-8 encoding
« Last Edit: April 14, 2024, 09:45:44 AM by Bilgus »
Logged

Offline chris_s

  • Developer
  • Member
  • *
  • Posts: 299
Re: Converting ID3 image tags from PNG to JPG
« Reply #8 on: April 14, 2024, 10:51:43 AM »
Try adding
Code: [Select]
id3v23_unsync => 0
to your config

Quote
Embedded JPEG images must not be unsynchronized.
https://download.rockbox.org/daily/manual/rockbox-ipodvideo/rockbox-buildap3.html#x19-447000C

https://github.com/Rockbox/rockbox/blob/b06073f7716373b115b4e039193ce622dbccb3a5/lib/rbcodec/metadata/id3tags.c#L302
« Last Edit: April 14, 2024, 11:06:11 AM by chris_s »
Logged

Offline wintermute23

  • Member
  • *
  • Posts: 122
  • What Would Batman Do?
Re: Converting ID3 image tags from PNG to JPG
« Reply #9 on: April 14, 2024, 12:14:13 PM »
Yes! That did the trick!

Thank you so much for the help. I had seen that line in the manual, but not understood what it meant, and not noticed the unsync option in MP3::Tag, which I might have been connect but didn't... But now I have a working proof-of-concept.

Now I just need to integrate it into the actual script, which should be easy enough. Once again, thanks.
Logged
Past: iRiver H320 w/ 32GB CF card mod; iPod 4th Gen 30GB; iPod 5th Gen 30GB; iPod Classic 80GB

Present: iPod Classic w/ 128GB SSD

Offline wintermute23

  • Member
  • *
  • Posts: 122
  • What Would Batman Do?
Re: Converting ID3 image tags from PNG to JPG
« Reply #10 on: April 14, 2024, 07:46:11 PM »
If anyone is interested, the full code for my homemade podcatcher (now with embedded album art formatted for RockBox) is here: https://github.com/wintermute115/podcasts
Logged
Past: iRiver H320 w/ 32GB CF card mod; iPod 4th Gen 30GB; iPod 5th Gen 30GB; iPod Classic 80GB

Present: iPod Classic w/ 128GB SSD

Offline chris_s

  • Developer
  • Member
  • *
  • Posts: 299
Re: Converting ID3 image tags from PNG to JPG
« Reply #11 on: April 14, 2024, 09:44:27 PM »
thanks for sharing  :)
Logged

  • Print
Pages: [1]
« previous next »
+  Rockbox Technical Forums
|-+  Support and General Use
| |-+  Audio Playback, Database and Playlists
| | |-+  Converting ID3 image tags from PNG to JPG
 

  • SMF 2.0.19 | SMF © 2021, Simple Machines
  • Rockbox Privacy Policy
  • XHTML
  • RSS
  • WAP2

Page created in 0.134 seconds with 21 queries.