Mp3 files are not being packaged
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:
This is the folder that contains the python script "createAnkiDeckTest.py":
I created the deck with the .zip extention to check its contents and this is what I found:
The media file only contains a json with the names of the audio files
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'.")
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.
Replace 'Avoir.mp3' with '[sound:Avoir.mp3]' PS: You do not need a separate audio field, you can add the sound to the front field or the back field of the card.
Replace 'Avoir.mp3' with '[sound:Avoir.mp3]'
I have been struggling with this kind of issue for several hours. Sometimes it works with "test.mp3," while other times it only works with "[test.mp3]." How can we resolve problems like this?
This works in this commit: f"sound:{data['Pronunciation']}",
but it only works with: f"[sound:{data.get('Pronunciation', '')}]", now.
By the way, using brackets like [] makes it look messy.
This is something to do with Ankii itself, you can test it in the desktop app as well by importing a sound file directly. It will add brackets. Media files require [] in order to work. They should not work without it.