Good Afternoon. I'm beginner in python. So, I was trying to make YouTube Video/Audio Downloader with PyTube (For Educational Purpose only). I have seen many videos on youtube and I was trying to make this tool better. So I have added video/audio choosing option and resulation/quality choosing option. The Good thing is I have successfully made Video downloader. But I got a problem in the Audio Downloader. The problem is, PyTube downloads the audio file in MP4 format. I have searched on google and youtube. But I could not find any solution. I want to rename the from mp4 to mp3 (cause the file is OK, but the format is wrong). as a beginner, I don't know how to save a downloading file in somewhere else (temporary folder), and rename it then transfer it to output folder. I tried to add filename=link.title+'mp3'. But It returns this error: OSError: [Errno 22] Invalid argument: 'G:/Downloaded Videos/Latest English Ringtone | Turkish Bgm Ringtone 2021 | Bad Boy | Attitude Tone | Villain Ringtone.mp3'
here is my Code:
from pytube import YouTube
link='https://www.youtube.com/watch?v=KrhPrPK2owA'
link=YouTube(link)
print('Title:',link.title+'\n'+'Views:',link.views)
streams=link.streams.filter(type='audio')
kbps_list=[]
itag_list=[]
print('Available Kbps: ',end='')
for s in streams:
i=s.itag
s=s.abr
if s not in kbps_list:
kbps_list.append(s)
itag_list.append(i)
print(s,end=' ')
reso=input('\nEnter Kbps to download: ')
if reso not in kbps_list:
print('This Kbps is not available')
from sys import exit
exit()
reso=kbps_list.index(reso)
final=streams.get_by_itag(itag_list[reso])
print('Downloading...')
final.download('G:/Downloaded Videos/')
# final.download('G:/Downloaded Videos/', filename=link.title+'mp3')======================
# if I add custom filename, It returns the error ========================================
print('Successfully Downloaded!')
Need to set the file name and can download in mp3 as you tried but it will still be in the mp4a codec. Not sure if that matters to you.
Your title is all weird and it includes the file path. Probably why link.title is not working. Try the below code to strip the title and the file path.
import os
head tail = os.path.split(link.title)
final=streams.get_by_itag(itag_list[reso]).download(filename=tail.strip(" | ") + ".mp3")
You can also set output directory by passing the argument output_path="some location".
It would look like:
final=streams.get_by_itag(itag_list[reso]).download(output_path="some location", filename=tail.strip(" | ") + ".mp3")
Related
Hello I am working on making a youtube video downloader, and a video converter to practice python language. With the video converter, I am trying to convert an mp4 file to a mp3 file. I want the user to input the specified path without having to open the text editor to do so. My issue is this. I am having trouble trying to figure out how I can possibly, if it is possible, to convert the file using the user's input. Can someone help me with this?
Here is the code I have so far:
import moviepy.editor as mp
#ask the user for the location of the file
loca = (input("Please input the location of the video file you wish to convert: "))
# Insert Local Video File Path
clip = mp.VideoFileClip(r'loca')
# Insert Local Audio File Path
clip.audio.write_audiofile(r'loca')
Just type extension to the file
If you can't imagine here's what's look like.
import moviepy.editor as mp
#ask the user for the location of the file
loca = (input("Please input the location of the video file you wish to convert: "))
filename = input("File name. : ")
# Insert Local Video File Path
clip = mp.VideoFileClip(loca)
# Insert Local Audio File Path
clip.audio.write_audiofile(filename)
I'm trying to convert DVD iso files to mp4 using HandbrakeCLI. I use the following line in a batch file:
D:\HandBrakeCLI.exe -i "D:\input.iso" -o "D:\output.mp4" --no-markers --width "720" --height "480" --preset "HQ 480p30 Surround" --encoder "mpeg2" --audio-lang-list "eng"
When I do this, I must then extract the audio from the file, using the following line:
D:\eac3to\eac3to.exe "D:\output.mp4" "D:\output.wavs" -down16
However, when I attempt to extract the audio, I get the error message
The format of the source file could not be detected.
Is there anything wrong with my former line of code that's causing the mp4 to get screwed up?
Minor side question: I'm also trying to get handbrake to remove subtitles and also only keep English audio, do you know what code could be used for that? I started a bit there with the --audio-lang-list "eng" but I'm now sure what to do from there.
Thanks a lot in advance!
You need to use a valid audio format. .wavs is not valid. You have to use an available audio codec to output to the below for --aencoder. The default output audio for MP4 is .aac
av_aac
copy:aac
ac3
copy:ac3
eac3
copy:eac3
copy:truehd
copy:dts
copy:dtshd
mp3
copy:mp3
vorbis
flac16
flac24
copy:flac
opus
copy
Defaults for audio
av_mp4 = av_aac
av_mkv = mp3
You need to pass none for no subtitles
-s none
And define only eng track like you were doing
--audio-lang-list eng
Check out the Handbrake CLI Documentation for the command line code:
https://handbrake.fr/docs/en/latest/cli/cli-guide.html
You can also try using a different program once you extract the audio. A program like XMediaRecode. It can also remux audio and video and convert other audio formats to wav
https://www.videohelp.com/software/XMedia-Recode
I want to convert any audio file (flac, wav,...) to mp3 with python
I am a noob , I tried pydub but I didn't found out how to make ffmpeg work with it, and If I'm right it can't convert flac file.
The idea of my project is to :
Make musicBee send the path of the 'now playing' track (by pressing the assigned shortcut) to my python file which would convert the music if it is not in mp3 and send it to a folder. (Everything in background so I don't have to leave what I'm doing to make the operation)
You can use the following the code:
from pydub import AudioSegment
wav_audio = AudioSegment.from_file("audio.wav", format="wav")
raw_audio = AudioSegment.from_file("audio.wav", format="raw",
frame_rate=44100, channels=2, sample_width=2)
wav_audio.export("audio1.mp3", format="mp3")
raw_audio.export("audio2.mp3", format="mp3")
You can also look here for more options.
flac_audio = AudioSegment.from_file("sample.flac", "flac")
flac_audio.export("sampleMp3.mp3", format="mp3")
I have 2 functions as below:
def select_audio():
os.chdir("/home/norman/songbook")
top1.lower(root)
name=tkFileDialog.askopenfilename()
doit="play " + name
top1.lift(root)
os.system(doit)
def select_video():
os.chdir("/home/norman/Videos")
top2.lower(root)
name=tkFileDialog.askopenfilename()
doit="mpv --fs " + name
top2.lift(root)
os.system(doit)
They are selected from buttons to allow choosing and playing audio files or video files.
They work to some extent.
Videos are in a different directory and at the same level as the audio files.
It doesn't matter which I choose first I see the correct directory so I can play say a video, if after it's finished I choose audio it still shows the video directory.
Similarly if I first choose audio it still shows the audio directory if I select videos.
I have no idea why it does this. I am not an experienced programmer as you can probably tell from the code.
Some suggestions:
Use a raw string to make sure that Python doesn't try to interpret anything following a \ as an escape sequence:
Change os.chdir("/home/norman/whatever") to os.chdir(r"/home/norman/whatever")
It won't solve this problem, but it will avoid you future problems.
For tkFileDialog use the initialdir option:
Change name=tkFileDialog.askopenfilename() to
name=tkFileDialog.askopenfilename(initialdir=r"home/norman/whatever", parent=root)
I'm doing a game with wx.python and when I try to add a background music using wx.Sound I got the following error:
Python error:
Sound file 'images/game.wav' is in unsupported format.
This is the code I use. I've seen this code many times on the web but none had
the same problem:
sound = wx.Sound("images/game.wav")
sound.Play(wx.SOUND_ASYNC)
Sounds like you need to save the WAV file in a different format. WAV files are not all created equal.
http://en.wikipedia.org/wiki/WAV#WAV_file_compression_codecs_compared
Why not use wx.media or maybe the mplayer widget? I have a tutorial on each of these:
http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/
http://www.blog.pythonlibrary.org/2010/04/20/wxpython-creating-a-simple-mp3-player/