Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mp3 files are not being packaged #140

Open
PineappleCourtesy opened this issue Jul 21, 2024 · 1 comment
Open

Mp3 files are not being packaged #140

PineappleCourtesy opened this issue Jul 21, 2024 · 1 comment

Comments

@PineappleCourtesy
Copy link

PineappleCourtesy commented Jul 21, 2024

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'.")
@flazouh
Copy link

flazouh commented Sep 30, 2024

Then, set the MyMedia field on your card to [sound:sound.mp3] for audio and for images (from the readme) I have an issue where my audio are still not getting exported though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants