I need to add thumbnails (album art) to mp3 (and other) files programmatically (using python) and save ffmpeg output in memory (to be able to send resulting bytes to another process).
Pay attention to '-f', 'mp3', 'pipe:1'
in last example please. Ffmpeg breaks working with pipe unless you set format for resulting stream.
What I have now: 1. This example bash oneliner works as expected making proper mp3 files with thimbnails (Telegram plays audio and shows thumbnail):
ffmpeg -i qwe.mp3 -i asd.png -acodec copy -map 0 -map 1 -disposition:v:1 attached_pic out.mp3
- This python code does the same with same result:
subprocess.run(['ffmpeg', '-i', 'qwe.mp3', '-i', 'asd.png', '-acodec', 'copy', '-map', '0', '-map', '1', '-disposition:v:1', 'attached_pic', 'out.mp3'])
- But I need bytes, not file on file system, so I do the following and get bytes. But then, if I save those bytes to file, it results to broken mp3 file. I cant play it with Deadbeef and Telegram.
audio_data = subprocess.run(['ffmpeg', '-i', 'qwe.mp3', '-i', 'asd.png', '-acodec', 'copy', '-map', '0', '-map', '1', '-disposition:v:1', 'attached_pic', '-f', 'mp3', 'pipe:1'], stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
len(audio_data.stdout)
2621690 # 2,6 Mbytes, it's ok
with open('out.mp3', 'wb') as f:
f.write(audio_data.stdout)
Last example gives me a broken mp3 file. It can't be played in deadbeef and Telegram.
I suggest that problem is somewhere around several streams (-map parameters) or/and with -f mp3
directive (since I have an image inside an mp3 stream or something like that).
I tried setting additional id2v3 tags and changing other options but no luck. Any suggestions in this ?