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

How should I download and get an m4a audio file from youtube video ? #26050

Open
sanfx opened this issue Jul 18, 2020 · 1 comment
Open

How should I download and get an m4a audio file from youtube video ? #26050

sanfx opened this issue Jul 18, 2020 · 1 comment
Labels

Comments

@sanfx
Copy link

@sanfx sanfx commented Jul 18, 2020

  • I'm asking a question
  • I've looked through the README and FAQ for similar questions
  • I've searched the bugtracker for similar questions including closed ones

Question

How can I download a youtube video to extract an audio to m4a container ?

I have written this python code

from __future__ import unicode_literals
import youtube_dl
import argparse
import sys
import os


HOME = os.getenv("HOME")

DOWNLOAD_TO = os.path.join(
	HOME, "Music/iTunes/iTunes Media/Automatically Add to iTunes/")


def get_parser():
	"""Get options to parse.
	"""
	parser = argparse.ArgumentParser()
	parser.add_argument("urls", nargs="+")
	parser.add_argument(
		"--format", 
		choices=('mp3', 'm4a'),
		default="mp3",
		help="file format of audio song.")
	parser.add_argument(
		"--quality",
		default='192',
		help="Quality for selected format.")
	return parser.parse_args()


class MyLogger(object):
    def debug(self, msg):
        print("> " + msg)

    def warning(self, msg):
        print("> "+ msg)

    def error(self, msg):
        print(msg)


def my_hook(d):
    if d['status'] == 'finished':
        print('Done downloading, now converting ...')


def run():
	args = get_parser()

	ydl_opts = {
	    'format': 'bestaudio/best',
	    'postprocessors': [{
	        'key': 'FFmpegExtractAudio',
	        'preferredcodec': args.format,
	        'preferredquality': args.quality,
	    }],
	    'logger': MyLogger(),
	    'progress_hooks': [my_hook],
	}
	os.chdir(DOWNLOAD_TO)
	with youtube_dl.YoutubeDL(ydl_opts) as ydl:
	    ydl.download(args.urls)

I tried to download m4a with quality 192
ydla https://www.youtube.com/watch?v=KkvSkWYobHk --format m4a

I am getting this error

File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 2018, in download
    res = self.extract_info(
  File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 808, in extract_info
    return self.process_ie_result(ie_result, download, extra_info)
  File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 863, in process_ie_result
    return self.process_video_result(ie_result, download=download)
  File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 1644, in process_video_result
    self.process_info(new_info)
  File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 2000, in process_info
    self.post_process(filename, info_dict)
  File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 2067, in post_process
    self.report_error(e.msg)
  File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 625, in report_error
    self.trouble(error_message, tb)
  File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 595, in trouble
    raise DownloadError(message, exc_info)
youtube_dl.utils.DownloadError: ERROR: audio conversion failed: [aac @ 0x7f973780f600] The encoder 'aac' is experimental but experimental codecs are not enabled, add '-strict -2' if you want to use it.

I am not sure if I can add strict through ydl object ! or if my options are incorrect.

Thanks
san

@sanfx sanfx added the question label Jul 18, 2020
@sanfx
Copy link
Author

@sanfx sanfx commented Jul 28, 2020

managed to get it to work by removing the postprocessor hook

'postprocessors': [{
	        'key': 'FFmpegExtractAudio',
	        'preferredcodec': args.format,
	        'preferredquality': args.quality,
	    }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.