简体   繁体   中英

Using FLAC decoder and LAME Encoder to convert FLAC files using Python

I am currently attempting to undergo a project to automate my music library cataloging using Python scripts, and I desperately need help before I go insane. For starters, I am using Linux (Arch Linux to be specific). To explain, I recently began using Morituri to rip my CD collection. I especially like the Mortituri script because it checks the files three times over, and generates FLAC files immediately.

This above system works fine, but I ran into another problem. I like FLAC files for my PC, but for mobile usage, I need MP3 files. I came across this famous little bash script:

for f in *.flac; do flac -cd "$f" | lame -b 320 - "${f%.*}".mp3; done

I saw this great little script, I knew there must be some way to replicate this in python. So I began working on a script that converts FLAC to MP3, and uses the basic arguments used by their respecitve CLI interfaces. I decided it be best to start with a single file at a time, and work my way up.After learning a bit about the subprocess module and pipes in python, I wrote the code below:

#!/usr/bin/python3

from subprocess import *

MyTestFile = ('/path/to/file.flac')

def Convert_It(File):
with open(File, 'r') as infile:
    FlacDecode = Popen(["flac", "-cd","-","-"],stdin=infile,stdout=PIPE)
    LameEncode = Popen(["lame", "-b", "192","-", "/a/differnet/path/test.mp3"],stdin=PIPE)

Convert_It(MyTestFile)

As it turns out, the output of this script is as follows:

flac 1.3.1, Copyright (C) 2000-2009  Josh Coalson, 2011-2014  Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
welcome to redistribute it under certain conditions.  Type `flac' for details.



-: ERROR while decoding metadata
state = FLAC__STREAM_DECODER_END_OF_STREAM
Warning: unsupported audio format

So I have a few questions:

  1. Why is FLAC acting this way? In testing the script, I set to path to a real FLAC file. Why it say this?

  2. How can I make this work?!

Really, any method using simple commands. Programs likes ffmpeg are not very easy to understand (how to set LAME q value to 0? No idea.) And other python scripts are way to long (like flac2all) for me to reverse engineer.

In review, any help is accepted. Just as long as it follows my specifications of: 1. only using the bare FLAC and LAME tools for linux (no sox or ffmpeg), 2. A script not too long. 3. Doesn't create a temporary WAV file. 4. Can encode any Flac file to MP3. I'm not even worried about metadata. Just the file itself.

Please HELP!

EDIT 08/05/2015:

I discovered I was using pipes incorrectly. Here is the code I created that solves my problem:

FlacDecode = subprocess.Popen(["flac","-cds",MyFile,"-"],stdout=subprocess.PIPE)
LameEncode = subprocess.Popen(['lame','-b','320','-'(NewFilePath)],stdin=FlacDecode.stdout,stdout=subprocess.PIPE)
EndLame = LameEncode.communicate()[0]

I had a similar problem, converting flac files to mp3, and found a one line bash solution here which reads:

for f in *.flac; do ffmpeg -i "$f" -aq 1 "${f%flac}mp3"; done

I appreciate that this doesn't answer the question but it is an elegant solution to your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM