Support » Plugin: Media Library Assistant » Extract chapter info from mp3?

  • Resolved tobaco

    (@tobaco)


    Hi,
    thanks for this great plugin!
    Is it possible to extract informations about chapters from mp3 and store this info in a custom field?

    Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for your question and for your patience while I was travelling and away from my computers.

    It is possible to store metadata in MP3 files using the “ID3” standard. WordPress core includes limited support for ID3 information and it may be possible to extract the data you want. I have not worked with this data and would have to investigate further.

    Can you provide me a link to one or more MP3 files that include the chapter information you want to extract? I will copy them to my system and perform some tests. Thanks for any examples and additional information you can provide.

    Thread Starter tobaco

    (@tobaco)

    Thanks for your response and your offer to investigate!
    Here is some more information about chapters in mp3: http://id3.org/id3v2-chapters-1.0

    And here is an mp3 to test: https://www.dropbox.com/s/0cnmzph9ui6cvd3/2019_destilliert_kw36.mp3?dl=0

    Thanks!

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update with the link to more information and a file to test with. I am traveling and away from my development system, but I was able to make a copy of the MP3 file on my laptop.

    WordPress core includes a partial version of a popular tool, getID3(). If this tool contains functions for extracting the chapter information I may be able to add them to MLA’s extraction features. I will have a look when I return home next week, and I will post an update here when I have progress to report. Thanks for your patience.

    Thread Starter tobaco

    (@tobaco)

    Thanks for your support and your help with this!
    It’s much appreciated.

    Plugin Author David Lingren

    (@dglingren)

    Thanks again for waiting. I uploaded the example file you posted and analyzed it. The chapter information is available in the “chapters” element of the “id3v2” array, which you can access in a number of ways. I used the [mla_gallery] shortcode and the mla_caption parameter for some quick analysis.

    If you code:

    [mla_gallery post_mime_type=all ids=6390 mla_caption='{+id3:id3v2.chapters,export+}']

    You can display all the chapters formatted as an array dump:

    array ( 0 => array ( ‘time_begin’ => 9006, ‘time_end’ => 41568, ‘chapter_name’ => ‘Begrüßung’, ), 1 => array ( ‘time_begin’ => 41568, ‘time_end’ => 431204, ‘chapter_name’ => ‘destilliert ab sofort monatlich’, ), 2 => array ( ‘time_begin’ => 431204, ‘time_end’ => 597178, ‘chapter_name’ => ‘Flugscham beim eigenen Urlaubsflug’, ), 3 => array ( ‘time_begin’ => 597178, ‘time_end’ => 683498, ‘chapter_name’ => ‘Ausblick auf shots im September’, ), 4 => array ( ‘time_begin’ => 683498, ‘time_end’ => 776455, ‘chapter_name’ => ‘Midsommar bald im Kino’, ), 5 => array ( ‘time_begin’ => 776455, ‘time_end’ => 866014, ‘chapter_name’ => ‘Mailverhalten im Urlaub’, ), 6 => array ( ‘time_begin’ => 866014, ‘time_end’ => 1022148, ‘chapter_name’ => ‘Brexit als Nachrichtenthema’, ), 7 => array ( ‘time_begin’ => 1022148, ‘time_end’ => 1086384, ‘chapter_name’ => ‘Wanda in der detektor.fm Session’, ), 8 => array ( ‘time_begin’ => 1086384, ‘time_end’ => 1123433, ‘chapter_name’ => ‘Ad Astra mit Brad Pitt’, ), 9 => array ( ‘time_begin’ => 1123433, ‘time_end’ => 1151000, ‘chapter_name’ => ‘Verabschiedung’, ), )

    If you code:

    [mla_gallery post_mime_type=all ids=6390 mla_caption='{+id3:id3v2.chapters.*.chapter_name+}']

    You can display a comma-separated list of the chapter names:

    Begrüßung, destilliert ab sofort monatlich, Flugscham beim eigenen Urlaubsflug, Ausblick auf shots im September, Midsommar bald im Kino, Mailverhalten im Urlaub, Brexit als Nachrichtenthema, Wanda in der detektor.fm Session, Ad Astra mit Brad Pitt, Verabschiedung

    If you simply code:

    [mla_gallery post_mime_type=all ids=6390 mla_caption='{+id3:id3v2.chapters+}’]

    You can display the uninteresting result:

    Array, Array, Array, Array, Array, Array, Array, Array, Array, Array

    Your original question was “Is it possible to extract informations about chapters from mp3 and store this info in a custom field?” You can define a custom field mapping rule for this purpose. For example:

    1. Navigate to the Settings/Media Library Assistant “Custom Fields” tab.
    2. Scroll down to the “Add New Custom Field Rule” section on the left-hand side of the page.
    3. Select the name of the custom field you want to edit in the “Name” dropdown or click “Enter new field” and type the name of a new custom field.
    4. Change the Data Source dropdown to “- Template (see below) -”.
    5. Enter “([+id3:id3v2.chapters+])” in the Meta/Template text box.
    6. Check the “MLA Column” box if you want to display the field as a column in the Media/Assistant submenu table.
    7. Leave the “Quick Edit” box unchecked.
    8. Leave the “Bulk Edit” box unchecked.
    9. Select “Replace” for “Existing Text”.
    10. Select “Raw” for “Format”.
    11. Select “Array” for “Option”.
    12. Click “Add Rule” to create the new rule.

    The above rule will populate the custom field with a single value containing a real, serialized PHP array that you can use with your own custom code in any way you like. It’s not very useful unless you write some code to process it, though. You can experiment with the “Multi” option, which will create a separate value for each of the chapters. You can also experiment with the other Format and Option settings to see what makes sense for your application.

    I hope that gives you the information you need to add chapter information to your application. I am marking this topic resolved, but please update it if you have any problems or further questions regarding the above suggestions. Thanks for your patience and for your interest in MLA.

    Thread Starter tobaco

    (@tobaco)

    WOW, thanks for your research and help! This is so great! I’m kind of speechless …

    This works as you described it.

    Just 2 last questions:

    1. I checked the box “Delete NULL Values Do not store empty custom field values” but my new custom field is now also saved with every upload which does not contain id3-tags or chapters (images etc.). It’s just an empty field there. I thought this checkbox would prevent this. Is this a bug or does this checkbox has another meaning.

    2. With every upload of a mp3 with a cover image, the image from this file get’s also added to the media library and is set up as featured image to the mp3 file entry. Is there a way to disable this? I already unchecked the checkboxes “Enable Featured Images” and “Enable Featured Image Generation”, but this seems to have no effect.

    Thanks again and kind regards!

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update with the positive feedback and the good news. Thanks as well for your two questions.

    Regarding the “empty fields”, I was able to reproduce that on my system. If you change the “Format” value from “Raw” to “Native” you can avoid the problem. After you change the rule, click the “Purge Values” and then the “Execute” rollover actions to re-apply it to all your items.

    Regarding the cover images, this is a WordPress feature independent of MLA; I was able to reproduce it without MLA or any other plugins active. You can see the code that generates the items in the wp_generate_attachment_metadata() function, which is located in wp-admin/includes/image.php.

    Thread Starter tobaco

    (@tobaco)

    Hi David,

    thanks for your response and looking clarifying the issue with the cover images (sorry for blaming your plugin 😉 ) and pointing out the solution with the native option.

    Since the arrays are stored as a string in the post meta field, do you have a solution to convert them back to a “real” array?
    I know, I could use eval(), but this seems kind of evil.

    Right now I’m using this function to convert the string to an array:

    function strtoarray($a, $withKeys = true){
            $arr = [];
            $a = ltrim($a, '[');
            $a = ltrim($a, 'array(');
            $a = ltrim($a, 'array (');
            $a = rtrim($a, ']');
            $a = rtrim($a, ')');
            $tmpArr = explode(",", $a);
            foreach ($tmpArr as $v) {
                if($withKeys){
                    $tmp = explode("=>", $v);
                    $k = $tmp[0]; $nv = $tmp[1];
                    $k = trim(trim($k), "'");
                    $k = trim(trim($k), '"');
                    $nv = trim(trim($nv), "'");
                    $nv = trim(trim($nv), '"');
                    $arr[$k] = $nv;
                } else {
                    $v = trim(trim($v), "'");
                    $v = trim(trim($v), '"');
                    $arr[] = $v;
                }
            }
            return array_filter($arr);
        }

    This works, but feels hacky. If I think about it right now, it would be nice to store this data as a json string in the database. Would this be possible?

    Thanks!

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update, and thanks for your positive review of MLA. Good reviews and positive feedback are great motivators to keep working on the plugin and supporting its users.

    You wrote “Since the arrays are stored as a string in the post meta field…“. On my system, with “Format: Native” and “Option: Array” the column content is stored (by WordPress, not MLA) as a PHP serialized array. Here is the content for your example file:

    a:10:{i:0;s:95:"array (
      'time_begin' => 9006,
      'time_end' => 41568,
      'chapter_name' => 'Begrüßung',
    )";i:1;s:113:"array (
      'time_begin' => 41568,
      'time_end' => 431204,
      'chapter_name' => 'destilliert ab sofort monatlich',
    )";i:2;s:117:"array (
      'time_begin' => 431204,
      'time_end' => 597178,
      'chapter_name' => 'Flugscham beim eigenen Urlaubsflug',
    )";i:3;s:114:"array (
      'time_begin' => 597178,
      'time_end' => 683498,
      'chapter_name' => 'Ausblick auf shots im September',
    )";i:4;s:105:"array (
      'time_begin' => 683498,
      'time_end' => 776455,
      'chapter_name' => 'Midsommar bald im Kino',
    )";i:5;s:106:"array (
      'time_begin' => 776455,
      'time_end' => 866014,
      'chapter_name' => 'Mailverhalten im Urlaub',
    )";i:6;s:111:"array (
      'time_begin' => 866014,
      'time_end' => 1022148,
      'chapter_name' => 'Brexit als Nachrichtenthema',
    )";i:7;s:117:"array (
      'time_begin' => 1022148,
      'time_end' => 1086384,
      'chapter_name' => 'Wanda in der detektor.fm Session',
    )";i:8;s:107:"array (
      'time_begin' => 1086384,
      'time_end' => 1123433,
      'chapter_name' => 'Ad Astra mit Brad Pitt',
    )";i:9;s:99:"array (
      'time_begin' => 1123433,
      'time_end' => 1151000,
      'chapter_name' => 'Verabschiedung',
    )";}
    

    If you use the WordPress get_post_meta() function to retrieve the content comes back to you as a real PHP array, not a string. If you have a string that looks like the above format you can use the WordPress function maybe_unserialize() to convert it to an array. Have a look at the source code for the WordPress get_metadata() function to see how it works.

    Does your system work differently from mine?

    Thread Starter tobaco

    (@tobaco)

    Hi and thanks for your reply.
    Yes, it’s stored as an array, but this array contains “subarrays” which are returned as a string. Here’s a var_dump from my get_post_meta function call:

    array(10) {
      [0]=>
      string(95) "array (
      'time_begin' => 9006,
      'time_end' => 41568,
      'chapter_name' => 'Begrüßung',
    )"
      [1]=>
      string(113) "array (
      'time_begin' => 41568,
      'time_end' => 431204,
      'chapter_name' => 'destilliert ab sofort monatlich',
    )"
      [2]=>
      string(117) "array (
      'time_begin' => 431204,
      'time_end' => 597178,
      'chapter_name' => 'Flugscham beim eigenen Urlaubsflug',
    )"
      [3]=>
      string(114) "array (
      'time_begin' => 597178,
      'time_end' => 683498,
      'chapter_name' => 'Ausblick auf shots im September',
    )"
      [4]=>
      string(105) "array (
      'time_begin' => 683498,
      'time_end' => 776455,
      'chapter_name' => 'Midsommar bald im Kino',
    )"
      [5]=>
      string(106) "array (
      'time_begin' => 776455,
      'time_end' => 866014,
      'chapter_name' => 'Mailverhalten im Urlaub',
    )"
      [6]=>
      string(111) "array (
      'time_begin' => 866014,
      'time_end' => 1022148,
      'chapter_name' => 'Brexit als Nachrichtenthema',
    )"
      [7]=>
      string(117) "array (
      'time_begin' => 1022148,
      'time_end' => 1086384,
      'chapter_name' => 'Wanda in der detektor.fm Session',
    )"
      [8]=>
      string(107) "array (
      'time_begin' => 1086384,
      'time_end' => 1123433,
      'chapter_name' => 'Ad Astra mit Brad Pitt',
    )"
      [9]=>
      string(99) "array (
      'time_begin' => 1123433,
      'time_end' => 1151000,
      'chapter_name' => 'Verabschiedung',
    )"
    }

    Every chapter is an array as a string. That’s what I meant. Sorry for the confusion.

    Plugin Author David Lingren

    (@dglingren)

    Thank you for clarifying the issue, and I regret that I didn’t look carefully enough at the field content to discover this MLA defect on my own. I have corrected the problem so that the nested subarrays are also returned as PHP arrays, not strings.

    I have uploaded a new MLA Development Version dated 20190923 that contains the fix. You can find step-by-step instructions for using the Development Version in this earlier topic:

    PHP Warning on media upload with Polylang

    It would be great if you can install the Development Version and let me know if it works for you. Thanks for your help and persistence.

    Thread Starter tobaco

    (@tobaco)

    Thanks for the fix, which works perfectly!
    I think we can close this issue now and mark it as resolved.

    Thanks again!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Extract chapter info from mp3?’ is closed to new replies.