Skip to content

Mp3 files are not being packaged #140

@PineappleCourtesy

Description

@PineappleCourtesy

Hello! I came across this wonderful project during my language-learning joruney. I gave it a try and it worked just fine until I tried to add .mp3 files to my cards. I was testing the whole thing of creating decks and cards with the help of chat gpt.

This is what shows up when I try to use the deck in AnkiDroid:

Screenshot_20240721_054516_AnkiDroid

This is the folder that contains the python script "createAnkiDeckTest.py":

ScreenCapture

I created the deck with the .zip extention to check its contents and this is what I found:

ScreenCapture2

The media file only contains a json with the names of the audio files

ScreenCapture3

How can I succesfully package the audio files soo I can use them in my cards in AnkiDroid? Thank you for your time!

This is my code:

import genanki
import os

# Define the model (card type) for the deck
my_model = genanki.Model(
    1600000000,
    'Simple Model',
    fields=[
        {'name': 'Front'},
        {'name': 'Back'},
        {'name': 'Audio'},
    ],
    templates=[
        {
            'name': 'Card 1',
            'qfmt': '''
                <div style="text-align: center; font-size: 24px;">
                    {{Front}}
                    <br>
                    <audio controls>
                        <source src="{{Audio}}" type="audio/mpeg">
                        Your browser does not support the audio element
                    </audio>
                </div>
            ''',
            'afmt': '''
                <div style="text-align: center; font-size: 24px;">
                    {{Back}}
                    <br>
                    <audio controls>
                        <source src="{{Audio}}" type="audio/mpeg">
                        Your browser does not support the audio element
                    </audio>
                </div>
            ''',
        },
    ],
)

# Define the deck
my_deck = genanki.Deck(
    44584545,
    'My First Deck',
)

# Define the notes (cards) to add to the deck
notes = [
    genanki.Note(
        model=my_model,
        fields=['Hello', 'Bonjour', 'Avoir.mp3'],  # Ensure filenames match
        tags=['greetings'],
    ),
    genanki.Note(
        model=my_model,
        fields=['Goodbye', 'Au revoir', 'Associez.mp3'],  # Ensure filenames match
        tags=['farewell'],
    ),
]

# Add notes to the deck
for note in notes:
    my_deck.add_note(note)

# Create the package (export file)
my_package = genanki.Package(
    my_deck,
)

# Specify media files with correct relative paths
my_package.media_files = [
    'Avoir.mp3',
    'Associez.mp3',
]

# Save the package to an .apkg file
my_package.write_to_file('my_deck.zip')

print("Deck has been created and saved as 'my_deck.apkg'.")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions