3

What I would like to do is get the metadata from audio or video files and save it to a database record, so far the only way to do this seems to save AVCONV to a file using a subprocess.Open call then read that file, is there any libraries that can do this to save some steps? I couldn't find a way to do it with Pydub or PySox. Here is my simplistic hamfisted beginner code I used that does work and puts the bitrate, duration, etc info into a variable audio_info and the metadata into metadata. OGG output worked differently than the other formats i tested (which was a ton of video and audio!).

    try:
            p = subprocess.Popen(["avconv" , "-i" , music_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            retcode = p.wait()
    except IOError,e:
            pass
    extension = uploaded_music_file[-3:]
    if "ogg" not in [err , extension]:
            if "Metadata:" in err:
                    list = err.split("Metadata:")
                    holder = list[1].split("Duration:")
                    metadata = holder[0]
                    audio_info = holder[1].replace("At least one output file must be specified","")
                    print metadata
                    print audio_info
            else:
                    list = err.split("Duration:")
                    audio_info = list[1].replace("At least one output file must be specified","")
                    print "No Metadata"
                    print audio_info
    else:
            list = err.split("Duration:")
            if "Metadata:" in list[1]:
                    data = list[1].split("Metadata:")
                    metadata = data[1].replace("At least one output file must be specified","")
                    audio_info = data[0]
                    print metadata
                    print audio_info
            else:
                    audio_info = list[1].replace("At least one output file must be specified","")
                    print "No Metadata"
                    print audio_info
if (audio_info):
            print "AUDIO INFO:"
            cursor.execute("UPDATE songDB SET audio_info = %s WHERE id = %s" ,[ audio_info , song_id ] )
            if (metadata):
                    print "METADATA:"
                    cursor.execute("songDB pack_song SET metadata = %s WHERE id = %s" ,[ metadata , song_id ] )
llogan
  • 121,796
  • 28
  • 232
  • 243
Fight Fire With Fire
  • 1,626
  • 3
  • 19
  • 28

1 Answers1

5

Pydub does include a function for retrieving metadata, pydub.utils.mediainfo("/path/to/file"), though pydub is only designed to deal with audio files. However the current implementation of mediainfo doesn't seem to be doing anything audio specific, so it may work for you.

It returns a dictionary of metadata as provided by ffmpeg/libav (specifically the ffprobe / avprobe utility)

>>> from pydub.utils import mediainfo
>>> mediainfo("/path/to/file.mp3")

which, for the test1.mp3 file used by pydub's unit tests, returns:

{
  u'DISPOSITION': {
    u'attached_pic': u'0',
    u'clean_effects': u'0',
    u'comment': u'0',
    u'default': u'0',
    u'dub': u'0',
    u'forced': u'0',
    u'hearing_impaired': u'0',
    u'karaoke': u'0',
    u'lyrics': u'0',
    u'original': u'0',
    u'visual_impaired': u'0'
  },
  u'TAG': {u'encoder': u'Lavf55.12.100'},
  u'avg_frame_rate': u'0/0',
  u'bit_rate': u'96179',
  u'bits_per_sample': u'0',
  u'channel_layout': u'stereo',
  u'channels': u'2',
  u'codec_long_name': u'MP3 (MPEG audio layer 3)',
  u'codec_name': u'mp3',
  u'codec_tag': u'0x0000',
  u'codec_tag_string': u'[0][0][0][0]',
  u'codec_time_base': u'1/32000',
  u'codec_type': u'audio',
  u'duration': u'10.044000',
  u'duration_ts': u'141740928',
  u'filename': u'/Users/jiaaro/Documents/code/pydub/test/data/test1.mp3',
  u'format_long_name': u'MP2/3 (MPEG audio layer 2/3)',
  u'format_name': u'mp3',
  u'id': u'N/A',
  u'index': u'0',
  u'max_bit_rate': u'N/A',
  u'nb_frames': u'N/A',
  u'nb_programs': u'0',
  u'nb_read_frames': u'N/A',
  u'nb_read_packets': u'N/A',
  u'nb_streams': u'1',
  u'probe_score': u'51',
  u'profile': u'unknown',
  u'r_frame_rate': u'0/0',
  u'sample_fmt': u's16p',
  u'sample_rate': u'32000',
  u'size': u'120753',
  u'start_pts': u'487305',
  u'start_time': u'0.034531',
  u'time_base': u'1/14112000'
}   
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • Pydub is excellent! Oneliner to convert an alac file including tags and cover two mp3: `AudioSegment.from_file(src).export(dst, format='mp3', tags=mediainfo(src)['TAG'])` – Toby May 22 '17 at 10:50